views:

225

answers:

3

It seems folks have lost the ability to generate old style COBOL/RPG reports using modern day languages.

I often see code using DataReaders that relies on a count of records. So an additional aggregrate query is present when it need not be.

In most circumstance this code needs to know if there is another record available or not. In other words, tell me if I'm at the last record so I can display a record separator.

A simple algorithm would be as follows:


Dim available As Boolean = rdr.Read()
While available
  DisplaySearchRecord(rdr)
  available = rdr.Read()
  If available Then DisplaySeparator()
End While

Please, do not use COUNT(*) or a datatable/dataset when a simple change in algorithm will suffice.

A: 

Why not showing the separator after the while?

   While rdr.Read()
       DisplaySearchRecord(rdr)
   End While
   DisplaySeparator()
Eduardo Molteni
A: 

You can try something like this

Dim IsFirst As Boolean = True

While rdr.Read()
  If Not IsFirst
    DisplaySeparator()
  Else
    IsFirst = False
  End If

  DisplaySearchRecord(rdr)
End While
Kibbee
A: 

you have to keep calling reader.Read() and it will return false when there are no more records.

So what I would do is dump the data out of the db into a List<YourRecord> and after I have the list populated ... iterate through it with a for( i++ ) loop and check i against the list.Count

Chad Grant