Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I tried to connect DB2 database using ADO.Net in C#. It connected successfully. However, I would like to close the database at 'finally' block. When I tried to close the connection at "finally" block, it is showing an error saying "Use of unassigned local variable"

 

Cant we close the connection in Catch or finally part in Try & Catch block. I have pasted my code below. Error is coming in the catch and finally section

 

Am I doing in wrong way?

 


private void button1_Click(object sender, EventArgs e)
       {
           OdbcConnection MyDBConn;
           String ConnString;
           try
           {
               ConnString = "Dsn=comp1;uid=use1;pwd=pwd1;mode=SHARE;dbalias=advcare;patch2=6;patch1=1024;lobmaxcolumnsize=1048575;longdatacompat=1";
               MyDBConn = new OdbcConnection(ConnString);
               MyDBConn.Open();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error in Connection" + ex.ToString());
           }
           finally
           {
               MyDBConn.Close();

           }

                      
       }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...