This Code is easy way to load / show data in Datagrid. This Code write in vb.net 2003 and use sqlserver 2000 as database. I use module to connected VB.Net with SqlServer 2000. so the function to connected can be use in every form in this project.
Show Data in DataGrid with VB.Net 2003 and SQLServer 2000
'This Code needed one DataGrid On Form.
'In Module
'Declare outside of class
Imports System.Data
Imports System.Data.SqlClient
Module Koneksi
Public conn As SqlConnection
Public Function GetConnect()
conn = New SqlConnection("server = MyServerName;database = MyDatabaseName;Trusted_Connection = yes")
Return conn
End Function
End Module
'In Form
'Declare outside of class
Imports System.Data
Imports System.Data.SqlClient
'Procedure To show data in DataGrid
Private Sub ShowDataGrid()
Dim conn As SqlConnection
Dim cmdStudent As New SqlCommand
Dim daStudent As New SqlDataAdapter
Dim dsStudent As New DataSet
Dim dtStudent As New DataTable
conn = GetConnect()
Try
cmdStudent = conn.CreateCommand
cmdStudent.CommandText = "SELECT * FROM YourTableName"
daStudent.SelectCommand = cmdStudent
daStudent.Fill(dsStudent, "YourTableName")
dgStudent.DataSource = dsStudent
dgStudent.DataMember = "YourTableName"
dgStudent.ReadOnly = True
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
End Try
End Sub
' Call procedure ShowDataGrid() on event Form Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowDataGrid() ' Load data to view in data grid
End Sub
' You can change the event to call this procedure like in button pressed and etc..
ITKnight 1 Light Poster
kimhanu -3 Newbie Poster
chrisdent1986 0 Newbie Poster
diorbabej 0 Newbie Poster
Jx_Man 987 Nearly a Senior Poster Featured Poster
cindi_rella 0 Newbie Poster
Lovealious 0 Newbie Poster
Jx_Man 987 Nearly a Senior Poster Featured Poster
kentlytears 0 Newbie Poster
neronero 0 Newbie Poster
neronero 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.