Question : Close SQL connection

Hi there

I have this code, my problem is when I have lots of users accessing the site, I get

the connection was not closed. The connection state is opened --> The connection was not closed. The connections current state is open.

How to i change the code so that it opens and closes the connection as quickly as possible?

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
internal static void uploadFile(Attachment aAttachment)
    {

      using (SqlConnection objConnection = new SqlConnection(_connectionstring))
      {
        using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
        {
          objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
          objConnection.Open();
          objCommand.ExecuteNonQuery();
        }
      }
    }

Answer : Close SQL connection

Sorry it should be like below, not as above..
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
internal static void uploadFile(Attachment aAttachment)
    {

      using (SqlConnection objConnection = new SqlConnection(_connectionstring))
      {
        using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
        {
          objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
          objConnection.Open();
          objCommand.ExecuteNonQuery();          
        }
      }
      finally
     {
      objConnection.Close();
     }
}
Random Solutions  
 
programming4us programming4us