DataReader Troubles

G

Greg Herrell

I think I must be going crazy?!
I am using a DataReader (OleDb in this case) to select some records from a
table. When I let the code execute it runs fine (via an ASP.Net page).
When I step through it using the debugger - the DataReader acts very
strange... My table has 5 records and while debugging it will iterate 2 out
of the 5 records before the call to Read() returns false.

This has been driving me nuts for a few hours now - hoping I am just really
beat and doing something obvious. Any insights, etc would be greatly
appreciated.
Below is a simplified block of code that reproduces the issue on my machine.

OleDbConnection oConn = new OleDbConnection(
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"] )
;
oConn.Open();
OleDbCommand oCmd = new OleDbCommand("SELECT * FROM tblClients Order By
ClientID", oConn);
OleDbDataReader oReader = oCmd.ExecuteReader();
string a = "";

//--this block executes correctly unless I decide to step through it
//--the results read "1,2,3,45," when I just let it go
//--when I step through it I get "2,5,"
while( oReader.Read() )
{
a = oReader.GetValue(0).ToString();
Response.Write( a + "," );
}

oReader.Close();
oConn.Close();

Thanks,
G. Herrell
 
S

Scott M.

I recently went through this myself and found that stepping through the code
(and adding watches and such) cause the DataReader to invoke its Read method
prematurely. In other words, the debugging is actually causing the problem.


Greg Herrell said:
I think I must be going crazy?!
I am using a DataReader (OleDb in this case) to select some records from a
table. When I let the code execute it runs fine (via an ASP.Net page).
When I step through it using the debugger - the DataReader acts very
strange... My table has 5 records and while debugging it will iterate 2 out
of the 5 records before the call to Read() returns false.

This has been driving me nuts for a few hours now - hoping I am just really
beat and doing something obvious. Any insights, etc would be greatly
appreciated.
Below is a simplified block of code that reproduces the issue on my machine.

OleDbConnection oConn = new OleDbConnection(
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"] )
 

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

Top