Datasheet
System.Data.OleDbDataReader dataReader = new _
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (!dataReader.HasRows)
Response.Write("No rows found");
else
while (dataReader.Read())
Response.Write(dataReader("ProductName") + "<br />";
dataReader.Close();
This first uses the HasRows property to determine if there are any rows, and then uses the Read method
to read a row. This is done within a loop, with
Read returning the true if there is a current row and
moving onto the next, and
false if there are no rows.
Summary
The results of the examples in this chapter have been relatively simple, but you've actually learned a lot.
The first three main topics looked at how to use the Web Matrix to reduce your development time,
taking away much of the legwork you'd normally have to do. We looked at the using the
Data Explorer to
drag and drop tables directly onto page, using the Web Matrix template pages, and using the code
wizards.
After looking at the quick way of getting data, you saw the theory behind it, examining the objects. Even
though we continued to use the wizards to generate code, you were now able to see how this wizard
code worked (just because we understand how it works doesn't mean we abandon anything that makes
our job easier).
Now it's time to look at taking your data usage one step further by showing how to update data, and
how to manage your data-handling routines.
Exercises
1. In this chapter we created a page that showed only the products for a selected category. Try and
think of ways to enhance this to show products for either a selected category or all categories.
2. In Exercise 1, we wanted to bind data from a database to a DropDownList as well as manually
add an entry. There are two ways to solve this issue – using techniques shown in this chapter,
and using techniques not yet covered. Try and code the solution using the known technique, but
see if you can think of a way to solve it using a new technique.
281
Reading from Databases
57084_08.qxp 30/01/2004 8:03 PM Page 281