Professional Documents
Culture Documents
NET
Part 3 of 4 - ADO.NET and Data
Binding (in C#)
Table of Contents
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#) ............. 1
Exercise 1 Using ADO.NET to retrieve data and display it in a control .......................................................................2
Exercise 2 Databinding to a DropDownList..................................................................................................................4
Exercise 3 Binding data to a templated control .............................................................................................................5
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#)
Page 1 of 5
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#)
Exercise 1
Using ADO.NET to retrieve data and display it in a control
Scenario
In this exercise, you will create a new ASP.NET Web Project with Visual Studio .NET 2003 and use ADO.NET to
populate a ListBox control with the contents of the Authors table in the pubs database.
Page 2 of 5
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#)
Tasks Detailed Steps
au_id FROM authors", conn))
{
conn.Open();
SqlDataReader reader =
cmd.ExecuteReader();
while (reader.Read())
authorsListBox.Items.Add(new
ListItem(reader.GetString(0),
reader.GetString(1)));
reader.Close();
}
This uses a SqlConnection, SqlCommand, and SqlDataReader to retrieve all of the
authors from the pubs database and insert each author's name as a new ListItem in
the authorsListBox control.
6. Compile and test! a. In the Solution Explorer right-click Default.aspx and select Set As Start Page.
b. Click Debug | Start Without Debugging (or press Ctrl-F5) to run the page.
c. Verify that the list box is populated with all of the authors from the Pubs database
and close the Internet Explorer window.
Page 3 of 5
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#)
Exercise 2
Databinding to a DropDownList
Scenario
In this exercise, you will use the ADO.NET DataSet class to retrieve the contents of the Authors table and bind the
results to a DropDownList.
Page 4 of 5
Fritz Onion’s Intro to ASP.NET Part 3 of 4 - ADO.NET and Data Binding (in C#)
Exercise 3
Binding data to a templated control
Scenario
In this exercise, you will trying binding the same DataSet you populated from the last part to a DataList, and will
define an ItemTemplate to render the elements of the query.
Page 5 of 5