0

Running the below code is giving me an error:

.NET Framework execution was aborted by escalation policy because of out of memory. System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

I have the following code all over the place to run my sql:

sql.Append(string.Format("SELECT TableId FROM ps_SavedTables WHERE guid = '{0}'", guid));

    using (IDataReader reader = SqlHelper.GetDataReader(sql.ToString())) {
        if (reader.Read()) {
            result = reader.IsDBNull(0) ? string.Empty : reader[0].ToString();
        }
        //CDW added to close SqlDataReader
        reader.Close();
    }

The GetDataReader is defined by this:

public static SqlDataReader GetDataReader(string sql, string connectionString) {
    lock (_lock) {
        SqlConnection connection = null;
        try {
            connection = GetConnection(connectionString);
            //connection.Open();
            using (SqlCommand cmd = new SqlCommand(sql, connection)) {
                WriteDebugInfo("GetDataReader", sql);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
        catch (Exception e) {
            if (connection != null)
                connection.Dispose();
            throw new DataException(sql, connectionString, e);
        }

    }
}
3
  • And at what line is that exception thrown? Commented May 23, 2012 at 3:32
  • Can you give us a bit more detail about the exception? Where it's thrown? Stack trace etc? Commented May 23, 2012 at 8:23
  • possible duplicate of Deallocation of SqlDataReader Commented May 23, 2012 at 19:24

1 Answer 1

1

The lock needs to be at the reader level... Multiple threads are opening readers

Sign up to request clarification or add additional context in comments.

2 Comments

@Totero I'm glad someone had the manners to comment. Thanks. Poor stuff from the op. Spam merchant.
Oh you like what he's selling! It brings the boys to the yard apparently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.