Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / October 2004

Tip: Looking for answers? Try searching our database.

Urgent...Please Help...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Irfan Akram - 29 Oct 2004 12:53 GMT
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.G. Ryan eMVP - 29 Oct 2004 14:21 GMT
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.
Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> I keep getting this irritating exception on and on. Please Help.
>
[quoted text clipped - 44 lines]
>
> Irfan
Irfan Akram - 29 Oct 2004 15:39 GMT
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

> 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
[quoted text clipped - 14 lines]
> sure that you're states are what you expect them to be.  Also, I'd use
> CommandBehavior.CloseConnection w/ the reader.
W.G. Ryan eMVP - 29 Oct 2004 23:39 GMT
Are the assertions failing or passing?  The one w/ Closed should be failing

Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> Thanks Sir,
>
[quoted text clipped - 56 lines]
> > sure that you're states are what you expect them to be.  Also, I'd use
> > CommandBehavior.CloseConnection w/ the reader.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.