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

datareader.read()

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Hello all-

I was wondering if anyone out there has figured out the .read() feature of the datareader for indicating an empty datareader.

For instance, in asp classic, you just ask:

if not (rs.eof and rs.bof) then
'you have data and can then:
while not rs.eof
'do your stuff here
rs.movenext
wend
else
'you have an empty recordset so:
response.write("Empty recordset")
end if

However, now, we don't get that .eof property of the datareader and when you ask for:

if datareader.read()

then you've already moved past your first record, since .read is sort of a double shot... it moves the record and returns a result (boolean).

The best I've been able to come up with for this type of test is the following:

Dim i As Integer = 0
While datareader.read()
i += 1
'you have data, so do what you need to do
End While
If i = 0 then
'you don't have data so:
someLabel.text = "Sorry, no data"
end if

But this seems like a hack to me -- notice the extra if block there to just check for an empty datareader at the end...

Anyone know of a better way to accomplish this test?

Thanks! :)
paul
penny1.gif
penny1.gif
 
Hey paul,

What do you mean by the datareader takes a "double hit"? Do you mean that it counts the header row as row one in the reader, so you always end up with one row?

Jack
 
No, I mean that when you call it, it moves the cursor.

So that if you ask:

if datareader.read() then
'you missed a row here, since when you
' call it again, the cursor moves again
while datareader.read()
'you're on the second row here
end while
else
'you have an empty set
end if

So if you have an empty set, then yes, the above example will work. But if it's not empty, then the logic falls apart because you miss the first row of your datareader in order to jump in the loop there.
penny1.gif
penny1.gif
 
hmmm, k

One way you could do it (but its just as hackish, just less code) is to maybe grab the name of the first column (never worked with datareaders, so this is assuming they won't return a row of column headers) and test to see if its null or not. If it is, then there aren't any rows returned. If there is, then there is some data (again, going with the previous assumption). So the code would look like

If Not (reader.GetName(0) Is Nothing) Then
While reader.read
blah blah blah
end while
else
label.text = "No Data"
End If

The other alternative you could do (but which means alot more code) is to write your own datareader class, and have total control over the reader.read, as well as add other functions (like a reader.recordcount or something). There's an article in the vs.net index that shows how to make one.

Jack
 
Interesting. I'll give it a shot.

I disagree on the hackish comment, though... this would be using a built-in property of the datareader to see if it was empty or not, which would be exactly what I'm after.

I'll let you know.

thx-
paul
penny1.gif
penny1.gif
 
Heh, sorry, what I meant by "hackish" was that:
"It's not as nice as using something like a reader.recordcount property, but it works...in theory"
;)

Jack
 
:-(

Just got around to testing this theory. Sadly, it doesn't work.

I honestly think it's a total oversight on MS's part not to give some .empty() query or something on it, and we're just going to have to hack it till they get around to upgrading the object.

penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top