Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

There is already an open DataReader on the MySQL connection

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

Short answer

One MySQL connection runs one operation at a time. The error appears when you run another command while a reader is still open on the same connection, or when several tasks share one connection. Read the rows into memory and close the reader first, or give each task its own connection.

  • Applies to MySqlConnector 2.x, Connector/NET 8.0, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Search for commands that run inside a loop over a reader, or for Task.WhenAll over methods that share one connection.

    grep -rn "ExecuteReader\|Task.WhenAll" --include=*.cs .
  2. 2Match what you see, then jump to its fix

    1. 1The failing line sits inside a while (reader.Read()) loopA second command runs while the first reader is still openFix
    2. 2The error is random and shows up under load or with Task.WhenAllSeveral tasks share one connectionFix
    3. 3The connection is a field or a static that other code also disposesA connection or command is disposed while still in useFix

What does the error look like?

  • There is already an open DataReader associated with this Connection which must be closed first
  • The error appears inside a while (reader.Read()) loop

Cause 1 of 3

A second command runs while the first reader is still open

1Check

MySqlConnector's guidance names this as nested use: executing a command on the same connection while a MySqlDataReader is still open.

grep -rn -A8 "while (reader.Read" --include=*.cs .

2Fix

Collect the values you need from the reader into a list, close the reader, then run the follow-up commands.

var ids = new List<object>();
using (var select = new MySqlCommand("SELECT id FROM orders", connection))
using (var reader = select.ExecuteReader())
{
    while (reader.Read())
        ids.Add(reader.GetValue(0));
}

foreach (var id in ids)
{
    using var update = new MySqlCommand("UPDATE orders SET seen = 1 WHERE id = @id", connection);
    update.Parameters.AddWithValue("@id", id);
    update.ExecuteNonQuery();
}

3Verify

The loop finishes with no exception.

dotnet run

Confirmed by mysqlconnector.net

Did this fix it?

Cause 2 of 3

Several tasks share one connection

1Check

The same page says a connection can run only one operation at a time, and that starting several operations on one connection at once, for example with Task.WhenAll, is misuse.

grep -rn "Task.WhenAll" --include=*.cs .

2Fix

Await the operations one after another on the shared connection. If you really need them to run at once, open one connection per task.

await using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();

using (var cmd = new MySqlCommand("SELECT 1;", connection))
    await cmd.ExecuteNonQueryAsync();
using (var cmd = new MySqlCommand("SELECT 2;", connection))
    await cmd.ExecuteNonQueryAsync();

3Verify

Repeat the load that failed. The error no longer appears.

dotnet run

Confirmed by mysqlconnector.net

Did this fix it?

Cause 3 of 3

A connection or command is disposed while still in use

1Check

The troubleshooting page lists disposing a connection or command while an operation is still running as the third misuse pattern.

grep -rn "static MySqlConnection\|\.Dispose()" --include=*.cs .

2Fix

Create the connection in a using or await using block close to where it is used, and await each operation before anything it depends on is disposed.

await using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
await using var command = new MySqlCommand("SELECT SLEEP(1)", connection);
await command.ExecuteScalarAsync();

3Verify

The operation completes, and the connection is closed once, at the end of the block.

dotnet run

Confirmed by mysqlconnector.net

Did this fix it?

How do I stop it coming back?

  • Do not keep one static MySqlConnection for the whole app; open a connection per unit of work.
  • Never run a command inside a loop that is still reading from a reader on the same connection.
  • Await every database call; do not start several on one connection.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.