Flylib.com

Books Software

 
 
 

Common Mistakes

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

Common Mistakes

As in the previous chapter, We're going to conclude with a list of common pitfalls that you might encounter while experimenting with the examples presented in this chapter:

  • The web server control in your HTML page must have an ID that matches the object name you've used in the DataSource and DataBind() statements. It must also have the runat ="server" attribute set.

  • Never instantiate a data reader object. We can let the command object's ExecuteReader() method do that job for us.

  • As always, know your data source. If you're getting an error, double check that you have the table and field names correct in your SQL query.

  • Your <asp:CheckBoxList> control must be in a <form> tag, and the <form> tag must also have the runat="server" attribute.

  • If you don't assign values to the DataValueField and DataTextField properties of the web server control, you won't get an error, but all the check boxes on your screen will be labeled System.Data.Common.DbDataRecord .

  • Note that the JET provider deletes spaces from column names, so Category ID will be displayed in the browser as CategoryID , without the space.

  • The DataValueField and DataTextValue properties must be set before the connection object's Close() method is called. Don't try to reduce the time your connection is open by setting these properties after the connection closes .

  • Similarly, the call to DataBind() must occur after DataValueField and DataTextField have been set.

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

Summary

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.