• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Can't close connection to database from finally block

Pertains to C#...
Hello,
I am unable to close the connection to my database from the finally block or another method. I am using the following code:

string ConStr = LoginFirst.Connection;
string SQL = "SELECT * FROM tableA";
         
try
   {
      OleDbConnection Conn = new OleDbConnection(ConStr);                  
      leDbCommand1 = new OleDbCommand(SQL, Conn);
      da = new OleDbDataAdapter();
      da.SelectCommand = oleDbCommand1;
      oleDbCb = new OleDbCommandBuilder(da);
      da.Fill(dataSet1, "Stats");
      dataGrid1.SetDataBinding(dataSet1, "Stats");

      }
      catch
      {
         //error handling code here                  
      }
      finally
      {
         //Want to close the DabtaBase connection here.  da.Close does not work here
                      
      }

I am also calling da.Update(dataSet1, "Stats"); from within another method. da.Close does not work here either.

Thanks
0
Saxitalis
Asked:
Saxitalis
2 Solutions
 
sabeeshCommented:
OleDbConnection Conn = new OleDbConnection(ConStr);                  

try
   {
     
      leDbCommand1 = new OleDbCommand(SQL, Conn);
      da = new OleDbDataAdapter();
      da.SelectCommand = oleDbCommand1;
      oleDbCb = new OleDbCommandBuilder(da);
      da.Fill(dataSet1, "Stats");
      dataGrid1.SetDataBinding(dataSet1, "Stats");

      }
      catch
      {
         //error handling code here                  
      }
      finally
      {
         //Want to close the DabtaBase connection here.  da.Close does not work here
                     
      }

You should place the con object outside the try block
0
 
RubenvdLindenCommented:
What are you trying to close? You name the connection object, but reference the DataAdapter object.
You simply cannot close an OleDbDataAdapter because the method does not exists, so da.Close() should raise an exception. Instead, when you're finished with da you should Dispose() it to free up memory.

If you want to close the connection object (Conn), you should declare it above the try block like this:


string ConStr = LoginFirst.Connection;
string SQL = "SELECT * FROM tableA";
OleDbConnection Conn;

try
{
      Conn = new OleDbConnection(ConStr);                  
      OleDbCommand1 = new OleDbCommand(SQL, Conn);
      da = new OleDbDataAdapter();
      da.SelectCommand = oleDbCommand1;
      oleDbCb = new OleDbCommandBuilder(da);
      da.Fill(dataSet1, "Stats");
      dataGrid1.SetDataBinding(dataSet1, "Stats");

      }
      catch
      {
         //error handling code here                  
      }
      finally
      {
         Conn.Close();
      }


I hope this helps.
0
 
Bob LearnedCommented:
If you are using C# 2005, then you can include a 'using' block that will simplify this a lot.

Bob
0
 
udhayakumardCommented:
Hi,

You are not opening the connection with the database anywhere to close it.... And the OleDbDataAdapter does not contain any member as 'Close'

0
 
udhayakumardCommented:
Hi,

Try this:

string ConStr = LoginFirst.Connection;
string SQL = "SELECT * FROM tableA";
OleDbConnection Conn;
         
try
   {
      Conn = new OleDbConnection(ConStr);
      Conn.Open();          
      leDbCommand1 = new OleDbCommand(SQL, Conn);
      da = new OleDbDataAdapter();
      da.SelectCommand = oleDbCommand1;
      oleDbCb = new OleDbCommandBuilder(da);
      da.Fill(dataSet1, "Stats");
      dataGrid1.SetDataBinding(dataSet1, "Stats");

      }
      catch
      {
         //error handling code here                  
      }
      finally
      {
         //Want to close the DabtaBase connection here.  da.Close does not work here

        Conn.Close();              
      }
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now