Binding Controls to a Dataset
A DataSet can be used with an ASP.NET page, using the Repeater, DataList, and DataGrid controls in exactly the same way as with a DataReader.
The following example, displays the contents of the Employees database table in a DataGrid control.
| Example 53 ExpertDataGrid.aspx |
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim dstEmployees As DataSet
Dim conNorthwind As SqlConnection
Dim dadEmployees As SqlDataAdapter
' Creating the DataSet
dstEmployees = New DataSet()
conNorthwind=New SqlConnection("Server=localhost;
UID =sa; PWD=secret;Database=Northwind" )
dadEmployees = New SqlDataAdapter( "Select * From Employees", conNorthwind )
dadEmployees.Fill( dstEmployees, "Employees" )
' Binding dataset to DataGrid
dgrdEmployees.DataSource = dstEmployees
dgrdEmployees.DataBind()
End Sub
</Script>
<html>
<head><title>ExpertDataGrid.aspx</title></head>
<body>
<asp:DataGrid
ID="dgrdEmployees"
Runat="Server" />
</body>
</html> |
| |
The output of above example is shown below:

It is seen the the DataGrid control is bound to the Products database table with the following two lines of codes:
dgrdEmployees.DataSource = dstEmployees
dgrdEmployes.DataBind()
The first statement assigns the first table in the DataSet to the DataGrid control's DataSource property.
The second statement actually binds the data from the DataSet to the DataGrid control, populating the DataGrid with the items from the DataSet.
Understanding DataTables
The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable are the DataSet and the DataView.
When DataTable objects are accessed, note that they are conditionally case-sensitive. For example, if one DataTable is named "mydatatable" and another is named "Mydatatable", a string used, to search for one of the tables is regarded as case-sensitive. However, if "mydatatable" exists and "Mydatatable" does not, the search string is regarded as case-insensitive.
A DataTable is a memory-resident representation of a database table in a DataSet.
|