Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim connectionString As String = "Your_Connection_String_Here"
Dim query As String = "SELECT * FROM YourTable"
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(query, connection)
Dim adapter As New SqlDataAdapter(command)
Dim dataTable As New DataTable()
adapter.Fill(dataTable)
GridView1.DataSource = dataTable
GridView1.DataBind()
End Using
End Using
End Sub
End Class
In this example:
The
BindData
method is called in thePage_Load
event handler to populate the GridView with data when the page is first loaded.The
BindData
method establishes a connection to the database, executes the SQL query to fetch data, fills the retrieved data into a DataTable, sets the DataTable as the DataSource of the GridView, and finally binds the GridView to display the data.
Ensure to replace "Your_Connection_String_Here"
with your actual database connection string, and "YourTable"
with the name of your database table. Adjust the SQL query as needed to fetch the desired data from your database.