Beginning ASP.NET 2.0 and Databases (Wrox Beginning Guides)

Chapter 4 - Data Readers, Command Objects, and Web Server Controls
byJohn Kauffman, Fabio Claudio Ferracchiatiet al.?
Wrox Press ?2002

We've covered two major areas in this chapter: how to use data reader and command objects together, and how to display data in some of the controls that display multiple records.

The data reader is one of two ways to get data from a connection; the other is the dataset. Data readers use minimal resources, because they stream data from the data store into a display control, and then sever the data connection - the data doesn't have to sit in IIS memory. However, data readers can only read data (they can never write it), and you can only navigate forwards through the records. You can't create multiple tables or relationships, or read data from more than one source. In spite of these constraints, however, data readers are very useful - there are many web pages that just display data from a single table or stored procedure.

Data reader objects are closely linked to command objects for two reasons. First, the latter's Text property holds a description of the data to be read. Second, it is the command object's ExecuteReader() method that actually creates and initializes the data reader object. The Text property can store a table name, an SQL statement, or a stored procedure (or an Access query).

All data-aware ASP.NET controls can be categorized into one of two groups: single-value, and multiple-value. In this chapter, we looked at the most common multiple-value controls. To use them, we must add an element for the control to the body of the page, and then set the control's data source in the code. Last, we must execute a DataBind(). Remember that we can associate two fields with each item in the list: the DataTextField appears to the user, while the DataValueField is available to the programmer, behind the scenes.

Multiple selections are available for some list controls. After the user clicks, we go to a method that loops through each item in the list to see if it has been selected. If it has, then we generally concatenate the Value or Text onto a result string for future use. We must also be prepared to react to a lack of selection.

At the end of the chapter, we began our discussion of the DataGrid, one of the most powerful controls in ASP.NET. The grid takes a set of data, senses the number of rows and fields, and then automatically builds an HTML table to display the information. We can set several format properties for the entire table, and ASP.NET defines row types, such as Header and Item, to which we can assign styles too.

Категории