Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

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
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();
        }
      }
    }

Open in new window

Avatar of krunal_shah
krunal_shah

hi,

modify like,

objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
Try this... Close connection after execute.
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(); 
          objConnection.Close(); 
        } 
      } 
    }

Open in new window

Avatar of mousemat24

ASKER

is it same for code like this

internal static DataSet getDocOwner(int admid)
    {

      using (SqlConnection objConnection = new SqlConnection(_connectionstring))
      {
        using (SqlCommand objCommand = CreateSqlCommand("Categories", objConnection))
        {
          objCommand.Parameters.Add("@AdminID", SqlDbType.Int).Value = admid;
          using (DataSet objDS = new DataSet())
          using (SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand))
          {
            objDataAdapter.Fill(objDS);
            return objDS;
          }
        }
      }
    }

do I place

objConnection.Close();  
after return objDS;


so

return objDS;
objConnection.Close();  
Hi,

if you are using it like this,
  using (SqlConnection objConnection = new SqlConnection(_connectionstring))
      {
      }

you do not need to close it.

Thanks,
Krunal
Avatar of kaufmed
A DataAdapter should take care of opening and closing the DB connection for you (when you call Fill()). Are you sure you are not manually opening a connection somewhere else?
i have this code too, dont know if this helps?

private static string _connectionstring = "connString";

static DALClass()
    {
      ConnectionStringSettings conString = ConfigurationManager.ConnectionStrings[_connectionstring];
      if (conString == null) throw new ConfigurationErrorsException(string.Format("You must have a {0} connection string in the config file", _connectionstring));
      _connectionstring = conString.ConnectionString;
    }
It's always advisable to close a connection inside finally as below..,
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();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of waltersnowslinarnold
waltersnowslinarnold
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi i would prefer this code

internal static void uploadFile(Attachment aAttachment)
    {
SqlConnection objConnection ;
     try
      {
    objConnection = new SqlConnection(_connectionstring)
         using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
        {
          objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
          objConnection.Open();
          objCommand.ExecuteNonQuery();
        }
      }
        catch(Exception ex)
        {
        }
        finally
        {
        objConnection.Close();
          }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes zadeveloper, I looked at my code, and the problem was because of methods and variables set as static.

And kaufmed, you too were right, I shouldnt have to close it, because the using block should take care of it.

Thanks to all the people who helped me out

Take care
no problems ... good luck.
NP. Glad to help :)