Question

So basically I have two connections in my application:

conn = new MySqlConnection("server=" + ip + ";uid=" + user +";pwd=" + pass + ";database=" + db);
readconn = new MySqlConnection("server=" + ip + ";uid=" + user + ";pwd=" + pass + ";database=" + db);

I use conn only for execution statements, such as UPDATE and INSERT. Then I use readconn only whenever someone joins the server which deals with a SELECT statement.

Then this error occurs with seemingly no pattern to it, other than it's being caused by the same function every time (which does an INSERT query on a transaction on the conn connection).

There is already an open DataReader associated with this Connection which must be closed first.

What I don't understand is, this stack trace shows that this error occurred in a function that uses conn, not readconn. On conn there is never, ever a DataReader being associated with it. It's almost as if there is only one connection, but this is why I specifically open two different connections, one for reading and one for inserting.

Any help or suggestions would be appreciated, maybe I'm just missing out on something obvious here.

Was it helpful?

Solution

You need to close the connection with conn.Close().

Or better, use something like:

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
//Your code
}

using (MySqlConnection readConn = new MySqlConnection(connectionString))
{
//Your code
}

it will close automatically the connection ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top