Binding DataReader Results to a Web Control

The most common use for the DataReader is to bind it to Web controls, much in the same way as you'd bind a DataSet. Other than the fact that you've eliminated overhead by not creating a DataSet, binding to a DataReader is almost exactly the same as binding to a DataSet. The code in Listing 8.4 demonstrates how to bind the DataReader. When run, this Web form will appear identical to the one in Figure 8.1. Notice that in line 22, the DataReader object is passed directly to the DataSource of the DataGrid Web control.

Listing 8.4. Data Binding the DataReader Object to a Web Control
 <% @Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <HTML> <HEAD> ...

Get Sams Teach Yourself ADO.NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.