Solved

if No Record found, datareader giving Error Message

Posted on 2004-09-27
7
493 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
ID: 12163556
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
ID: 12163581
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
ID: 12164866
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
ID: 12165505
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

20 Experts available now in Live!

Get 1:1 Help Now