question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception: There is already an open DataReader associated with this Command which must be closed first

See original GitHub issue

.Net Version 4.6.1 Microsoft.Data.SqlClient 2.01 linq2db 3.1.6

With Oracle: no Problem With SQL-Server: Exception: There is already an open DataReader associated with this Command which must be closed first.

Code:

using (DbContext dbContext = new DbContext(dbProvider))
{
  IEnumerable<string> list = dbContext.MENUS_LOG.Select(obj => obj.DATATYPE).Distinct();
   foreach(string strItem in list)
   {
      dbContext.MENUS_LOG.Where(objWhere => objWhere.DATATYPE == strItem)
                .Update(dbContext.MENUS_LOG, objWhere => new MENUS_LOG{ DATATYPE = strItem + "X"});
   }
}

DataProvider:

Oracle:

m_IDataProvider = new LinqToDB.DataProvider.Oracle.OracleDataProvider(ProviderName.OracleManaged,
LinqToDB.DataProvider.Oracle.OracleVersion.v12);

SQL-Server:

m_IDataProvider = new LinqToDB.DataProvider.SqlServer.SqlServerDataProvider(
ProviderName.SqlServer,
LinqToDB.DataProvider.SqlServer.SqlServerVersion.v2017,
LinqToDB.DataProvider.SqlServer.SqlServerProvider.MicrosoftDataSqlClient);

ConnectionString:

Oracle:

@"USER ID=UserDummy;CONNECTION TIMEOUT=60;PASSWORD=XXXXXX;DATA SOURCE=PKSABNAHME12;STATEMENT CACHE SIZE=0;CONNECTION LIFETIME=0;INCR POOL SIZE=1;STATEMENT CACHE PURGE=True";

SQL-Server:

@"Data Source=MSTEST17\MSTEST17;Initial Catalog=DUMMYDB;Integrated Security=True;Connect Timeout=60;Multiple Active Result Sets=True;Multi Subnet Failover=True;";

Exception:

System.InvalidOperationException
HResult=0x80131509
Message=There is already an open DataReader associated with this Command which must be closed first.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at Microsoft.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() at LinqToDB.Data.DataConnection.ExecuteNonQuery(IDbCommand command) at LinqToDB.Data.DataConnection.ExecuteNonQuery() at LinqToDB.Data.DataConnection.QueryRunner.ExecuteNonQueryImpl(DataConnection dataConnection, PreparedQuery preparedQuery) at LinqToDB.Data.DataConnection.QueryRunner.ExecuteNonQuery() at LinqToDB.DataContext.QueryRunner.ExecuteNonQuery() at LinqToDB.Linq.QueryRunner.NonQueryQuery(Query query, IDataContext dataContext, Expression expr, Object[] parameters, Object[] preambles) at LinqToDB.Linq.QueryRunner.<>c__DisplayClass32_0.<SetNonQueryQuery>b__0(IDataContext db, Expression expr, Object[] ps, Object[] preambles) at LinqToDB.Linq.ExpressionQuery1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at LinqToDB.LinqExtensions.Update[TSource,TTarget](IQueryable1 source, ITable1 target, Expression`1 setter)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
MaceWinducommented, Nov 19, 2020

This is valid error. You cannot issue another query over connection, while previous one is still running.

You can fix it by fetching all data from first query using something like ToList or ToArray

foreach(string strItem in list.ToList())
0reactions
alianapalarcommented, Dec 19, 2022

you are a perfect detail i was about to go crazy thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

There is already an open DataReader associated with this ...
There is already an open DataReader associated with this Command which must be closed first. Update: stack trace added:
Read more >
Why do I get the error message 'There is already an open ...
This is caused if you are attempting to use the same DataReader more than once in your code without closing the previous Datareader....
Read more >
There is already an open DataReader associated with this ...
Close the DataReader & then SqlConnection, if there is an exception is not getting closed. best practices to write this code in try...
Read more >
[RESOLVED] There is already an open DataReader ...
InvalidOperationException : There is already an open DataReader associated with this Command which must be closed first. Source Error:
Read more >
Script Task: There is already an open DataReader ...
Script Task: There is already an open DataReader associated with this Command which must be closed first. ' A Microsoft platform for building ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found