There is already an open DataReader on the MySQL connection
There is already an open DataReader associated with this Connection which must be closed firstShort 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.
Fix it fast
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 .2Match what you see, then jump to its fix
- 1
The failing line sits inside a while (reader.Read()) loopA second command runs while the first reader is still openFix - 2
The error is random and shows up under load or with Task.WhenAllSeveral tasks share one connectionFix - 3
The connection is a field or a static that other code also disposesA connection or command is disposed while still in useFix
- 1
What does the error look like?
There is already an open DataReader associated with this Connection which must be closed firstThe 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 runConfirmed by mysqlconnector.net
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 runConfirmed by mysqlconnector.net
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 runConfirmed by mysqlconnector.net
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
EF Core logs 'An error occurred using the connection to database' with MySQL
Can't connect to MySQL server on 'host_name' (111)
This EF Core message is only a wrapper.
3 causes, each with a fixDatabases
DbContextOptionsBuilder has no UseMySql method
Cannot find the UseMysql method on DbContextOptions
UseMySql comes from a provider package, not from EF Core itself.
3 causes, each with a fixDatabases
C# INSERT fails with 'Fatal error encountered during command execution'
MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution
A fatal error during command execution means the connection broke while the INSERT was in flight.
3 causes, each with a fixDatabases
MySqlException: Timeout expired in a .NET app
MySql.Data.MySqlClient.MySqlException: Timeout expired
Timeout expired means a wait ended before it finished.
3 causes, each with a fixDatabases
C# cannot reach MySQL: connection attempt failed, no response in time
System.Net.Sockets.SocketException (10060)
SocketError 10060 means the connection attempt timed out, or the host did not respond.
3 causes, each with a fixDatabases
MySqlException: unable to connect to any of the specified MySQL hosts
Can't connect to MySQL server on 'host' (10061)
The connector could not open a TCP connection to the host and port in your connection string.
4 causes, each with a fix