Datareader problem - read() returns true, but doesn't branch correctly

  • Thread starter Thread starter Scott Shuster
  • Start date Start date
S

Scott Shuster

Hi,

I'm having some strange problems in VS employing a DataReader. Here's
a small snippet of the code:

....
objDR = objCMD.ExecuteReader()
If objDR.Read() Then
(true stuff)
Else
(false stuff)
End If
....

For some reason, I'm getting to the Read() statement in debug mode and
it's showing 'True', but then it branches right to the (false) code.

If you think that's strange, here's where it gets worse. I get
different results if I execute it straight through, as opposed to
stepping through it line-by-line.

Has anyone else seen strange results in VS, using a DataReader, and
debugging?

Thanks,

Scott Shuster
(e-mail address removed)
 
My first thought would be that code and debug information are out of sync
for some reason....

Try perhaps to delete the debug information to make sure you are using the
latest created... ??????

Patrice
 
That's because your debug code is calling .Read, which executes, and
advances the cursor. Then your code tries to, but it's already been moved to
the first row. And presumably there is no second row, so the executing code
gets False after calling Read.

The debugger is executing stuff for real, since it has no way to execute
something like that on an object and rolling it back -it can't possible. So
you really need to be careful what you put in there.
 
Scott:
This is expected as far as i'm concerned...If you put objDR.Read() in your
watch, it'll actually execute objDR.Read() to see the result of that
function...which means that in your code, when you do objDR.Read() the
single row has already been read (by the debugger) and thus returns false...

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
Hiya

Datareader hasrows property might help some. I usually test to see is
hasrows = true , then read() off the datareader if there are indeed rows.

HTH Lerp
 
Back
Top