'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..