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

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

I think that the error is raised on this line

con.Open(); // InvalidOperationException if it's already open

You are reusing a connection and you probably have not closed it last time

You should always close a connection immediately as soon as you are finished with it, best by using the using-statement

public void run_runcmd(string query)   
{
    using(var con = new SqlConnection(connectionString))
    using(var cmd = new SqlCommand(query, con))
    {
        con.Open();
        // ...
    }  // close not needed since dispose also closes the connection
}

 

2020-03-04