Click to See Complete Forum and Search --> : How to know if DataReader is empty


Dreamon
January 8th, 2003, 03:09 PM
Doesn anyone know a easy way to find out if a DataReader is empty (has no records)??

I have looked through the methods for the DataGrid object, but couldn't find anything that made sense.

What I want to do:

if the DataGrid has no records, I want to display a simple message, like "No records".
If it does have records, dipslay them.

PS. I use while (DataReader.Read()) to loop through the DataReader.

Anyone?

pareshgh
January 8th, 2003, 03:23 PM
you can make use of counter and if counter is 0 then you can show up the message.

Paresh

Dreamon
January 9th, 2003, 01:08 AM
Well yeah, but that means that I have to loop through the DataReader just to find out if it's empty or not and then go through it again if it's not empty.

Isn't there any other way of checking if it's empty, so that I don't have to go into a loop every time?

Thanks,

pareshgh
January 9th, 2003, 11:16 AM
till now there isn't a way/ i am not sure if there exists.

if anyone knows let us know

thanks
Paresh Gheewala

Dreamon
January 9th, 2003, 11:58 AM
It seems weird to me that there is no easy way of doing this. I mean, the DataReader object is used alot. It's easy to use and for connected datahandling it's perfect.

I don't know. It bugs me a whole lot!

pareshgh
January 9th, 2003, 12:07 PM
yeah ! its weird ! when the datareader is collecting data from the sql why can it have a method for length of records otherwise everything it does have. so for example if there are are million record we have to go thru loop 1 million time atleast for just checking if its empty or not.

COME ON
Microsoft must improve this DLL and add one more property to it.

Paresh

MartinL
January 9th, 2003, 12:36 PM
No it is not weird...

DataReader is used for reading a forward-only data retrieved from the sql server. It is optimized for the speed. Thats why it is not possible... You should use DataReader only if you want to iterate through all returned rows.

If you need other functionality, use DataSet. Get DataTable object you want and use Rows.Count property to determine the count of rows...

Additionaly: there is no need to look for such property in your case. The best solution is really to check the count of loop iterations; and after it, check if it is zero...

Martin

pareshgh
January 9th, 2003, 12:39 PM
thanks Martin,
good one.

Paresh

Dreamon
January 9th, 2003, 02:44 PM
Thank you. I still think it's weird that they didn't attach a simple RecordCount method to the DataReader, but...

Thanks again.

MartinL
January 10th, 2003, 06:12 AM
No it is not weird... It is due to the implementation details of DataReaders. DataReader offers higher performance because it uses SQL-server's native network data transfer protocol and reads data directly from a database connection. If they desided to implement it in this way, they don't have any method how to get count of records before they read all of them...

Additionally, use of such method is strongly against the phylosophy how DataReader should be used. There is only one way how to discover count of the records affected by DataReader object. It is: iterate through all of them...

Really use this class only if you need fast forward-only data cursor...

Martin

pareshgh
January 10th, 2003, 04:31 PM
yes Martin, you are true.
with out even iterating once also how can we know that datareader is empty. i think its a direct method and fastest one. even if we do iterate once and do nothing with records but just counting then it won't take much long.

Paresh

geraintj
February 7th, 2003, 06:25 AM
The DataReader.Read() method returns true if it contains records and false if it's empty.

Geraint

pareshgh
February 7th, 2003, 12:13 PM
yes,

for example

SQLDataReader r = ....

while (r.Read() )
{
... process records etc
r.getvalue(i).. something like that
}

if r.Read() will have record set then it will go inside the loop otherwise it will ASAP come out since r.Read() advanced the record which doesn't have enough records. so its something like endoffile...

:cool:

thanx
Paresh