Bug #42694 MySql.Data.MySqlClient.MySqlException: There is already an open DataReader assoc
Submitted: 9 Feb 2009 13:35 Modified: 11 Feb 2009 13:35
Reporter: Farhan Ali Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:My Sql Client Version 5.1.11 OS:Linux
Assigned to: CPU Architecture:Any

[9 Feb 2009 13:35] Farhan Ali
Description:
i have following code in C#

  if (conn.State != ConnectionState.Open)
                    conn.Open();
                MySqlCommand comm = new MySqlCommand(query, conn);
                comm.CommandTimeout = 120;
                
                int res = comm.ExecuteNonQuery();
                conn.Close();

this is the exception coming after executing this code

MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
   at MySql.Data.MySqlClient.MySqlCommand.CheckState()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
   at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection conn)
   at MySql.Data.MySqlClient.MySqlConnection.Open()

I am not using data reader in my code.ExecuteNonQuery() itself calls data reader as i have analyzed,I have read that this bug is already fixed but why this is coming for me again and again

How to repeat:
this code cal generate this exception

 if (conn.State != ConnectionState.Open)
                    conn.Open();
                MySqlCommand comm = new MySqlCommand(query, conn);
                comm.CommandTimeout = 120;
                
                int res = comm.ExecuteNonQuery();
                conn.Close();
[11 Feb 2009 13:35] Tonci Grgin
Hi Farhan and thanks for your report.

Unfortunately, the code snippet you provided does not raise exception here. I doubt it raises exception at your box too as it's pretty straight forward.

My environment:
  o MySQL server 5.1.30GA on W2K8 x64 localhost using c/NET 5.2 from SVN. Tested both debug and release version of c/NET driver.

Test case that succeeds each time it's run:
    MySqlConnection conn = new MySqlConnection();
    conn.ConnectionString = "DataSource=localhost;Database=test;UserID=*;Password=*;PORT=*;Allow Zero Datetime=True;Treat Tiny As Boolean=True";
    if (conn.State != ConnectionState.Open)
        conn.Open();
    bool ok = true;
    MySqlCommand cmd = new MySqlCommand("SELECT 1", conn);
    cmd.CommandTimeout = 120;
    try
    {
        int res = cmd.ExecuteNonQuery();
    }
    catch (Exception)
    {
        ok = false;
        Console.Out.WriteLine("Exception " + e.ToString());
    }
    if (ok) Console.Out.WriteLine("succeded");
and the output:
---
The thread 0x21b4 has exited with code 0 (0x0).
'bug16126.vshost.exe' (Managed): Loaded 'F:\PublicShare\temp\bug16126\bug16126\bug16126\bin\Debug\bug16126.exe', Symbols loaded.
'bug16126.vshost.exe' (Managed): Loaded 'F:\PublicShare\temp\bug16126\bug16126\bug16126\bin\Debug\MySql.Data.dll', Symbols loaded.
'bug16126.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'bug16126.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_64\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'bug16126.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_64\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

succeded

The thread 0x1d20 has exited with code 0 (0x0).
The thread 0x2158 has exited with code 0 (0x0).
The program '[9192] bug16126.vshost.exe: Managed' has exited with code 0 (0x0).

Please either fix your code or post test case that actually has chance of failing and reopen the report.