Home >Database >Mysql Tutorial >Why Does `ExecuteNonQuery()` Throw \'There is already an open DataReader...\'?
DataReader Open During Connection Execution
While using a Visual Studio 2010/.Net 4.0 and MySQL project, a developer encountered the exception, "There is already an open DataReader associated with this Connection which must be closed first."
The code in question attempts to execute another SQL statement while a data reader is open. Specifically, an exception is thrown at the line cmdInserttblProductFrance.ExecuteNonQuery();.
This issue arises because the connection being used for both the data reader and the ExecuteNonQuery command. As per MSDN, such usage is unsupported:
"Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed."
To resolve this, close the data reader before attempting to execute the additional SQL statement.
The above is the detailed content of Why Does `ExecuteNonQuery()` Throw \'There is already an open DataReader...\'?. For more information, please follow other related articles on the PHP Chinese website!