Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for empty recordset 1

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
0
0
US
Remember how you would use:
Code:
if rs.EOF Then
  MsgBox "No records found..."
  Exit Sub 'jump out
End If
So as not to crash your program you jump out of the procedure and none of the code gets executed.

It seems as though this doesn't work in .NET. If there are no records it goes ahead and tries to execute the rest of the Sub and goes right ahead and crashes your program, showing that ugly error message that the user doesn't understand anyway. (unless you put On Error Resume Next)

In the following section of code...
Code:
   ds = New DataSet()
   cmd.Fill(ds)

   dt = New DataTable()
   dt = ds.Tables(0)

   numrows = dt.Rows.Count

I could test for 0 number of rows in the DataSet checking numrows = dt.Rows.Count, then if there were no records, I could display a message somewhere and jump out of the Sub so as not to crash the page.

Problem is that when I get to this line it crashes any way since there are no records. How do I get around this problem since there is no way of checking for end of file?

Thanks.

dpdoug
 
you might wanna use datareader instead..
dim dr as datareader
while dr.read
........
end while
or you can use
if dr.read
...
else
....
endif
 
Or you can run an ExecuteScalar("Select count(col_name) from TableName"),..) function to get the total number of rows affected. Check this value, if its > 0 then execute the code to return a dataset/datareader.

 
if ds.tables(0).rows.count > 0 then
dt = ds.tables(0)
numrows = dt.rows.count
else
'tell the user there was nothing there.
end if

HTH

Eva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top