none
Ado Connection and Try & Catch error RRS feed

  • Question

  • I am newbie to C#. Today I tried to connect DB2 database through ADO.Net. It connected successfully, however I want to close the database connection in 'finally' block, however it is giving 'Use of unassigned local variable" error.
    Bellow is the code. I am getting in Catch block after the message box and finally block too

    Cant we close database connection in the try & catch block?

    Code Snippet

    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());
                    MyDBConn.Close();

                }
                finally
                {
                    MyDBConn.Close();

                }

                           
            }


    Tuesday, February 26, 2008 8:45 PM

Answers

  • Yes, you should be able to close it.  That block of code should always execute irregardless of whether or not an exception was thrown or not.  You do not need the second line in the catch block.

     

    Which variable is being flagged  as 'unassigned'?  MyDBConn?  Also, SCOPE.  MyDBConn is initialized within the braces of your try block.  That initialization probably does not exist outside of the try block.  I would create and initialize MyDBConn and ConnString outside of the try block.

     

    Rudedog

    Tuesday, February 26, 2008 8:59 PM

All replies

  • Yes, you should be able to close it.  That block of code should always execute irregardless of whether or not an exception was thrown or not.  You do not need the second line in the catch block.

     

    Which variable is being flagged  as 'unassigned'?  MyDBConn?  Also, SCOPE.  MyDBConn is initialized within the braces of your try block.  That initialization probably does not exist outside of the try block.  I would create and initialize MyDBConn and ConnString outside of the try block.

     

    Rudedog

    Tuesday, February 26, 2008 8:59 PM
  •  Rudedog2 wrote:

    Yes, you should be able to close it.  That block of code should always execute irregardless of whether or not an exception was thrown or not.  You do not need the second line in the catch block.

     

    Which variable is being flagged  as 'unassigned'?  MyDBConn?  Also, SCOPE.  MyDBConn is initialized within the braces of your try block.  That initialization probably does not exist outside of the try block.  I would create and initialize MyDBConn and ConnString outside of the try block.

     

    Rudedog



    Thanks a lot. Eror came in myDBConn variable. As you suggested I created the variable and initilized outside of the Try block. It is working fine

    Thanks a lot again
    Tuesday, February 26, 2008 9:09 PM