Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2006, 09:08 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default DataReader Problem

Hello Friends,
I want to know What method should be use to see if there is any row selected in the DataReader or not? Is it DataReader.HasRows, DataReader.Count, or DataReader.Read.



Gaurav Jain
__________________
Gaurav
 
Old May 14th, 2006, 12:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Dear Gaurav:
DataReader is a readonly object to read the retrevied data from DB & u can not select a row of it,Its not elective u do it in a loop to do something with retrived data.
Could I ask what r u going to do?!

Always:),
Hovik Melkomian.
 
Old May 15th, 2006, 10:45 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

I want to check if DataReader contains data or not because it may be the case that DataReader may not retrieve any result and contains null value. Doesn't DataReader.HasRows and DataReader.Count exists??
 
Old May 15th, 2006, 11:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

There's a difference between whether the DataReader contains any record at all and if there is a record, whether some of its columns are null or not.

As of .NET 1.1, the data reader has an HasRows property that allows you to check whether any records have been returned, without actually looking at them. Alternatively, you can simply try to read from the reader:

If myReader.Read()
  'records found
End If

Once you have a record, you can use its IsDBNull method to determine whether a column in that record contains a null value or not.

The MSDN documentation for the DataReader provides a lot more information and examples.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old October 10th, 2007, 11:59 AM
Sjo Sjo is offline
Registered User
 
Join Date: Oct 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using Microsoft Enterprise Library for data access and I am using IDataReader to get the resultset. Since IDataReader doesnot have HasRows property I cannot check weather the reader returns result or not. If I use reader.Read(), it always returns 1 record less. I posted this issue earlier as well and i was suggested to implement HasRows by simply reading reader.Read() and setting the boolean value. But that also returns 1 record less. I am using Repeater control to display the result set.

Is there any other way I can check if the reader has records or not?

Thanks in advance

 
Old October 10th, 2007, 03:48 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote: posted this issue earlier as well and i was suggested to implement HasRows by simply reading reader.Read() and setting the boolean value.
That's not what we suggested. What we suggested was to keep track of a counter (for example) while reading.

Read() always eats a record. So if you use Read to check if there is a record you loose one. However, like this you won't:

bool hasRecords = false;
while (myReader.read())
{
  //Do whatever you need to do with myReader here
  hasRecords = true;
}

// Here you know whether there are records or not.

Otherwise, like I suggested in my article, check out the data-bound control after data binding.

Why did you move the discussion to this (old) thread? What was wrong with the other discussion?


Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Datareader problem is about to cause my death joer80 .NET Framework 2.0 1 June 7th, 2006 07:20 AM
Datareader problem, killing me joer80 ADO.NET 1 March 23rd, 2006 06:44 PM
DataReader problem nalla ASP.NET 1.0 and 1.1 Professional 6 February 7th, 2006 06:07 AM
Problem with datareader ashokparchuri ADO.NET 2 May 13th, 2005 04:32 AM
Problem reading DataReader after Databind rathbird ADO.NET 1 March 10th, 2004 05:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.