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

Hence, you can use the call to Read() as the condition in a while loop. If your query returns no results, the first call to Read() ends the loop before you do anything. If not, the code will keep looping until there are no more results. In short, your page needs to have this skeleton code in it:

//Create the DataReader by calling ExecuteReader()
SqlDataReader myReader = myCommand.ExecuteReader();

//Iterate through the DataReader in a while loop
while (myReader.Read())
{
.. processing instructions for each row in DataReader
}

//Close DataReader
myReader.Close();

Take care not to call Read() in the while statement and then again within the loop—say, in a method call—lest the code skip some of the results. It’s easy to do but hard to track down later in the code. Besides the actual data processing, it’s important you close the DataReader using Close() after you’ve finished with it. Once a DataReader has been opened through a connection, nothing else can use that connection until the DataReader is closed.

Even if an error occurs on that page, the connection is still isolated until the .NET garbage collector comes to dispose of the open DataReader. This is a needless waste of resources if all you have to do is make sure you close it. You also have a maximum number of connections that can be open at any one time, so under heavy loads, not closing your connections could actually generate errors, which is definitely not a good thing.

NOTE The examples in this chapter use the SqlClient data provider.You can find equivalent examples using the OLE DB and ODBC data providers in the code download for this book.

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