Solved

if No Record found, datareader giving Error Message

Posted on 2004-09-27
7
492 Views
Last Modified: 2007-12-19
Sir

For some purpose, I used datareader.  It is giving error messages, if no records avaialble.  In this case, any property or method is available to know datareader is blank.  Like record count or eof ......

Please help me
0
Comment
Question by:adwat
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
KGreg earned 168 total points
Comment Utility
do this:


while objReader.Read

     ' Loop through records.. objReader will be on the current record and this loop will execute as long as there is a record

End while


or if you're expecting a single record:

if objReader.read then
   ' Work on that one record then close the reader..
else

     '  No records.

end if


KGREG
0
 
LVL 6

Expert Comment

by:KGreg
Comment Utility
NOTE:

calling objReader.Read puts the objReader on the next record.  Initially, there is no current record.. so you need to call this to get to the first record.

The read function returns a boolean - True if there is a Next record.. and false if there is not.


KGREG
0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 166 total points
Comment Utility
You can also use HasRows property

if MyDataread.d.HasRows then
    MyDataread.Read
end if
0
 
LVL 18

Assisted Solution

by:DotNetLover_Baan
DotNetLover_Baan earned 166 total points
Comment Utility
You can do this :

Dim SqlDataReadr As SqlDataReader = SqlCommand1.ExecuteReader()
If SqlReader Is Nothing Then
   'No record to read
   Exit Sub
End If

-Baan
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now