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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check if the recordset is empty

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
How to check for empty recordset in VB.NET?

If rs is NULL does not work
 
D155:

If IsDBNull(RS(1).Value) Then Exit Do

I hope this helps.

Ron

Ron Repp
 
I am getting System.NullReferenceException when I try that
 
Try this:

If IsDBNull(RS(1).Value) Then
SomeVariable=string.empty
else
SomeVariable=RS(1).Value
end if



Ron Repp
 
I think it error because it tries to reference a value in the recordset(RS(1).Value) and the recordset is empty?
 
Try this:

RS.Open Table,Connection

If RS.RecordCount<=0 then
MessageBox.Show ("Empty Table")
else
Do While Not RS.EOF

If IsDBNull(RS(1).Value) Then
SomeVariable=string.empty
else
SomeVariable=RS(1).Value
end if
RS.MoveNext
Loop
end if

Ron Repp
 
Still no luck. I think if you try to access any property of an empty reordset it throws an error. i am looking for a way
to check if recordset is null without trying to access any of its properties
 
Mind showing us your code and what methods you're using? Recordsets are a bit "non-Netish", isn't it, are you really using it?

In VB(A) one would check the .EOF/.BOF properties of a recordset after opening.

[tt]if ((Not rs.bof) and (Not rs.eof)) then
' contains records
end if[/tt]

If you get errors when addressing one of it's properties, I'm wondering if it's instantiated.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top