This is very strange, it only happens sometimes although this piece of code is executed often.
I have this code running like every 5 seconds and it works fine, but sometimes i get the exception saved on my log.
Exception msg: The connection was not closed. The connection's current state is open.
public class Log
{
static SqlConnection sqlConnection{get; set;}
public Log()
{
sqlConnection = new SqlConnection(CNN_STRING);
}
public static void Save(){
StringBuilder sb = new StringBuilder();
sb.Append("UPDATE Blabla SET ...");
SqlCommand command = new SqlCommand(sb.ToString(), sqlConnection);
try
{
if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); }
sqlConnection.Open();
command.ExecuteNonQuery();
sqlConnection.Close();
}catch(Exception e){
Log.AddLog("Log ", 1, string.Concat("Query: ", sb.ToString(),
"Exception message: ", e.Message));
}
finally
{
if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); }
}
}
any clues?