Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidOperationException The connection was not closed. The connection's current state is open

Why does this code throw an Invalid Operation Exception?

private SqlCommand cmd; // initialized in the class constructor

public void End(string spSendEventNotificationEmail) {
  try {
    cmd.CommandText = spSendEventNotificationEmail;
    cmd.Parameters.Clear();
    cmd.Parameters.Add("@packetID", SqlDbType.Int).Value = _packetID;
    cmd.Parameters.Add("@statusID", SqlDbType.Int).Value = _statusID;
    cmd.Parameters.Add("@website", SqlDbType.NVarChar, 100).Value = Tools.NextStep;
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
  } finally {
    cmd.Connection.Close();
    cmd.Parameters.Clear();
    cmd.Dispose();
  }
  endCall = true;
}

InvalidOperationException

like image 767
jp2code Avatar asked Jun 15 '12 15:06

jp2code


2 Answers

You're trying to open a connection which is already open, this results in exception.

Solution 1 (recommended):

Inspect your code, check all the parts where cmd.Connection connection is opened and ensure that it's always closed properly.

Solution 2 (quick'n'dirty fix):

before line

cmd.Connection.Open();

add the following check/cleanup code:

if (cmd.Connection.State == ConnectionState.Open)
{
    cmd.Connection.Close();
}
like image 144
Sergey Kudriavtsev Avatar answered Oct 31 '22 22:10

Sergey Kudriavtsev


There's very little need for keeping the Sql* objects at the class level, especially based on what you're showing. You'll also lose the benefits of connection pooling by attempting to do it yourself.

With this method, you remove the possibility of your error because you're not sharing any objects

private readonly _connectionString = "...";

public void End(string spSendEventNotificationEmail) {
  using(var conn = new SqlConnection(_connectionString))
  using(var cmd = conn.CreateCommand())
  {
    cmd.CommandText = spSendEventNotificationEmail;
    cmd.Parameters.Add("@packetID", SqlDbType.Int).Value = _packetID;
    cmd.Parameters.Add("@statusID", SqlDbType.Int).Value = _statusID;
    cmd.Parameters.Add("@website", SqlDbType.NVarChar, 100).Value = Tools.NextStep;
    conn.Open();
    cmd.ExecuteNonQuery();
  }
  endCall = true;
}
like image 35
Austin Salonen Avatar answered Nov 01 '22 00:11

Austin Salonen