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 >>

Of course, this means there are pros and cons to using only DataReaders in your page. On the plus side, you have the following:

  • Using a DataReader is quick and efficient, as it doesn't need to worry about keeping track of every bit of data.
  • A DataReader doesn't need to store data in memory, so it uses fewer resources in creating a page.

That's not to say there aren't any disadvantages.

  • You can't do anything with the data such as sending changes back to the database. This would mean referring to data already passed through the reader, which isn't possible; DataReaders work only from database to client.
  • DataReaders require exclusive access to a connection. Once open, nothing else can use a connection until the DataReader is closed.

A DataReader isn't picky about the amount of data passed through it. You could request a single item of information from a field or the entire contents of the database. As long as you understand how to access it, it won't complain.

A DataReader is the resulting object from a call to ExecuteReader() on a

Command object.

SqlDataReader myReader = myCommand.ExecuteReader();

The general practice at this point is to assign (or bind) the values in a DataReader to controls on the Web form, and indeed that's what you've already done in the examples in Chapters 3 and 4. You've created a DataGrid control on the page, bound the data to it, and let ASP.NET take care of the display.

myGrid.DataSource = myReader;
myGrid.DataBind();

So far, all you've seen is the data displayed as a table thanks to that DataGrid, but you can bind information to several more data-aware Web form controls. For example, you can use a drop-down list, a set of radio buttons, or a calendar. We'll spend all of Chapter 7 on data binding, but there's another way to work with DataReaders that you'll look at here, and that's to iterate through them row by row.

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 >>