ADO.NET 101: Data Rendering with a DataList Control Introduction - Data from a DataReader
(Page 3 of 5 )
Getting a DataReader to read data from a table in a SQL 2000 Server database will be considered. The details of reading the data were described in two earlier tutorials and hence will be summarized here. The next picture shows the data link properties of the OleDB Connection. The provider used is the MS OleDB Provider for SQL Server. The Data Link Window is configured for connecting to the pubs database on the server.

The SQLConnection1 on the web form will have the following for its connection string:
workstation id=XPHTEK;packet size=4096;integrated security=SSPI;data source=XPHTEK; persist security info=False;initial catalog=pubs
Now let's place a SqlCommand1 control on the web page. Set the connection string of this command to the one that was first added. For the CommandText the following query was chosen.
SELECT fname, lname, emp_id, hire_date FROM employee ORDER BY
lname
Binding the DataList Control to dataThe DataList control is very similar to the DataGrid control. The DataList must be bound to the data source. Since we are using the DataReader as the data source, the DataList must be bound to the DataReader. The following code makes this possible.
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Create a new SQL connection
Dim conn As New SqlConnection
conn = SqlConnection1
'open connection
conn.Open()
'declare a Sql Command
Dim cmd As SqlCommand
cmd = SqlCommand1
'declare a DataReader
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
DataList2.DataSource = dr
'Bind data to control
DataList2.DataBind()
conn.close()
End Sub
Next: Designing the ItemTemplate >>
More Database Articles
More By Jayaram Krishnaswamy