Win a copy of Beginning ASP.NET 1.1. Databases

In some of the entries to our last competition a number of people asked us about various aspects of ASP.NET. There were even those <gasp> who suggested they used Visual Studio alongside Dreamweaver.

In immediate response, and in conjunction with Apress, we're giving you the chance to read a sample chapter from the forthcoming Beginning ASP.NET 1.1 Databases book by experienced .NET authors Dan Maharry and Damien Foggon.

Of course we've twisted the publisher's arms on your behalf, and have five books to give away to registered DMXzone members. All we want from you is to know either which bits of ASP.NET you've found most useful, or which you've found hardest to get to grips with. Lucky entries will be extracted by a draw from the inbox on the 22nd July!

If you want to pre-order a copy from Amazon >>

Iterating Through a DataReader

It may seem a waste of time to work through the results of a query row by row and work with each when you can just bind it to a control and let the control take care of it all, but consider that data isn't always for display. You may be using a database table to store user information and site preferences. Rather than displaying it on the screen, information from these tables may be assigned straightaway to controls' properties or stored in a business object for use across the whole site.

For example, you may create a Preferences object to store theme information for the whole site, store values from the database in its properties, and save it as a Session-level variable. Rather than accessing the database again, you just access the Session variable.

If any preferences are changed during the session, they're saved to the Session variable and when the session is over, the changes are sent back to the database. This minimizes both potential connections to the database for this purpose and also the overhead of using many Session variables at a time. You just use one with lots of information rather than several containing individual pieces of information.

To iterate through the contents of a DataReader, you use its Read() method. If you haven’t worked with Reader objects in general before, the idea is simple. A DataReader and DataSet reader has a pointer that you use to keep track of where you are in the information coming through your reader. If you like, it’s the same kind of thing that happens when someone uses their finger to keep their place on a page. Until they open the book and start to read, they can’t see anything.

The same thing applies in code. You can’t access anything until you call Read() the first time, and each time you call Read() after that, the DataReader lets another row through for you to use. Read() will also return a boolean value each time you call it: true if there’s another row for you to work with and false if you’ve reached the end of the query results, as shown in Figure 6-1.

Figure 6-1.Working through rows in a DataReader using DataReader.Read()


Ian Blackham

Ian BlackhamFollowing a degree in Chemistry and a doctorate in Scanning Tunneling Microscopy, Ian spent several years wrestling with acronyms in industrial R&D (SEM with a side order of EDS, AFM and TEM augmented with a topping of XPS and SIMS and yet more SEM and TEM).

Feeling that he needed a career with more terminology but less high voltages, Ian became a technical/commissioning editor with Wrox Press working on books as diverse as Beg VB Application Development and Professional Java Security. After Wrox's dissolution and a few short term assignments Ian became content manager at DMXzone.

Ian is a refugee from the industrial Black Country having slipped across the border to live in Birmingham. In his spare time he helps out with the website of a local history society, tries to makes sure he does what his wife Kate says, and worries that the little 'un Noah is already more grown up than he is.

See All Postings From Ian Blackham >>