Number of records in DataReader

  • Thread starter Thread starter Perre Van Wilrijk
  • Start date Start date
P

Perre Van Wilrijk

Hello,

I want to check whether my datareader object contains rows before continuing
and binding it to my ASP datagrid. I don't have to know how many, only if
there's at least 1 row. I've found some solutions in several newsgroups but
not the solution I'm looking for.

I've seen you can use a dataset and use it's rowcount property, but I don't
want to use a dataset only to know whether there are rows returned.
I've seen another solution using two queries, the first to get the number of
rows with SQL function count(*), followed by the same query to retrieve the
data.
I've seen a third solution in which you can bind your data to a datagrid and
than check the count property of the datagrid.
I've seen a fourth solution in which you loop through the records with a
read instruction but than you've to requery because the datareader is
forward only.

So, I'm not completely pleased with those solutions and wonder if there
isn't another way. In VB6 I used to check .eof property to check whether
there were rows returned, but it seems datareader hasn't that function. Is
there another way? If not ... what solution of the four above is the best??

Thanks a lot.
 
Hi,

call the datareader's Read method in a While() loop. If it contains atleast
1 record or more, it will return true, so that you can bound it to some
control.

For counting the actual number of rows the reader contains, there's no way
out as there are only 1 row in the memory of the reader at a time. Maybe
while looping through the while loop, using a counter you can get that.

HTH
Regards
Joyjit
 
In .NET Framework 1.1, the DataReader has a Has Rows property.

If (DataReader.HasRows)
---do something
 
Back
Top