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

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)
 
P

Patrice

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
 
M

Marina

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.
 
K

Karl Seguin

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!)
 
L

Lerp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top