Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection to SQL is not closing after call to DB in ASP.NET
    text
    copied!<p>I have a generic method to call a stored Procedure in ASP.NET:</p> <pre><code>public SqlDataReader ExecuteStoredProc(string sprocName, SqlParameter[] SqlP) { SqlDataReader iReader; SqlCommand sql = new SqlCommand(); sql.CommandText = sprocName; sql.CommandType = CommandType.StoredProcedure; sql.Connection = ConnStr; if (SqlP != null) { foreach (SqlParameter p in SqlP) { sql.Parameters.Add(p); } } sql.Connection.Open(); iReader = sql.ExecuteReader(CommandBehavior.CloseConnection); sql.Dispose(); return iReader; } </code></pre> <p>Even though I am calling CommandBehavior.CloseConnection the connection is not closing. I can get the data fine the first time I request a page. On reload I get the following error:</p> <blockquote> <p>The connection was not closed. The connection's current state is open. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p> <p>Exception Details: System.InvalidOperationException: The connection was not closed. The connection's current state is open.</p> <p>Source Error:</p> <p>Line 35: Line 36: } Line 37: sql.Connection.Open(); Line 38: iReader = sql.ExecuteReader(CommandBehavior.CloseConnection); Line 39: sql.Dispose();</p> </blockquote> <p>Finally if I put sql.Connection.Close(); before sql.Dispose(); I get an error that iReader is not readable because it's been closed already. </p> <p>Obviously I am closing my connection incorrectly, can someone point me in the right direction?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload