Urgent...Please Help...

G

Guest

I keep getting this irritating exception on and on. Please Help.


There is already an open DataReader associated with this Connection which
must be closed first.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: There is already an
open DataReader associated with this Connection which must be closed first.

Source Error:


Line 84: OdbcCommand myCommand = new
OdbcCommand(mySelectQuery,myConnection);
Line 85: myConnection.Open();
Line 86: OdbcDataReader myReader = myCommand.ExecuteReader();
Line 87: myReader.Close();
Line 88:

Here is that part of the source Code..

private void CheckUser()
{

string connectionString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=markingsystem;" +
"UID=akramm;" +
"PASSWORD=rootuser;";

string mySelectQuery = "SELECT * FROM account_details";
OdbcConnection myConnection = new OdbcConnection(connectionString);
OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);
myConnection.Open();
OdbcDataReader myReader = myCommand.ExecuteReader();
myReader.Close();


}

Note please that I only have 1 datareader, so the exception doesnt make
sense to me..


Your kind comments will be very much appreciated...

Thanks,

Irfan
 
W

W.G. Ryan eMVP

Although you're using a new connection (which should be working all else
being equal), I'd take the exception at face value. Do you have another
command somewhere that may be using the connection and may not be closing
it? Command objects use readers to do a lot of their work so it's possible
that another command is associated with this.

Is there something else going on after this or do you have any threads/async
stuff going on? Just to try to verify what's happening (and it's a good
habit), stick the connection declaraion in a using block
using(SqlConnection myConnection = new SqlConnection()){

}
Before open, use a Debug.Assert(myConnection.ConnectionState ==
ConnectionState.Closed);

Do the same right before you fire the exectueReader, just make sure to be
sure that you're states are what you expect them to be. Also, I'd use
CommandBehavior.CloseConnection w/ the reader.
 
G

Guest

Thanks Sir,

For your response..

The code is looking like this now.....

private void CheckUser()
{

string connectionString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=markingsystem;" +
"UID=akramm;" +
"PASSWORD=rootuser;";

string mySelectQuery = "SELECT * FROM account_details";

using(OdbcConnection myConnection = new OdbcConnection(connectionString))
{
OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);

Debug.Assert(myConnection.State == ConnectionState.Closed);
myConnection.Open();

Debug.Assert(myConnection.State ==
ConnectionState.Closed);
OdbcDataReader myReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);

myReader.Close();

}
}


Yet I keep getting the same error. Can you verify that this is how you
wanted the code to look like ? If yes then how can I get more information out
of this code and most importantly how can I resolve it ?

Thanks,

Irfan
 
W

W.G. Ryan eMVP

Are the assertions failing or passing? The one w/ Closed should be failing

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Irfan Akram said:
Thanks Sir,

For your response..

The code is looking like this now.....

private void CheckUser()
{

string connectionString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=markingsystem;" +
"UID=akramm;" +
"PASSWORD=rootuser;";

string mySelectQuery = "SELECT * FROM account_details";

using(OdbcConnection myConnection = new OdbcConnection(connectionString))
{
OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);

Debug.Assert(myConnection.State == ConnectionState.Closed);
myConnection.Open();

Debug.Assert(myConnection.State ==
ConnectionState.Closed);
OdbcDataReader myReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);

myReader.Close();

}
}


Yet I keep getting the same error. Can you verify that this is how you
wanted the code to look like ? If yes then how can I get more information out
of this code and most importantly how can I resolve it ?

Thanks,

Irfan
 

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

Similar Threads


Top