1 | initial version |
To solve the error message "The connection was not closed. The connection's current state is open" in a C# Winform, you need to ensure that you close the connection after using it. Here are some solutions:
SqlConnection connection = new SqlConnection(connectionString);
try
{
// Do something with the connection
}
finally
{
connection.Close();
}
using(SqlConnection connection = new SqlConnection(connectionString))
{
// Do something with the connection
}
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
By following one of these solutions, you can ensure that the connection is closed properly and avoid the error message.