Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code
C#
try
            {
                SqlCommand OpenCat = new SqlCommand(SQL, MainClass.Conn);
                SqlDataReader readCat = OpenCat.ExecuteReader();
                if (readCat.Read())
                {
                    cmbPettyCashCode.Text = MainClass.SetDefo(readCat.GetValue(0).ToString(), "");
                    txtCatName.Text = MainClass.SetDefo(readCat.GetValue(1).ToString(), "");
                    readCat.Dispose();
                }
                else
                {
                    readCat.Dispose();
                }
               
            }
            catch (Exception error)
            {
                
                cmbPettyCashCode.Text = "";
                MessageBox.Show(error.Message.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally {  }

I Can't See "OpenCat" and "readCat" at the catch point how to handle this

thanks
Posted
Updated 2-Mar-15 17:47pm
v2
Comments
[no name] 2-Mar-15 23:48pm    
What do u mean by .. I Can't See "OpenCat" and "readCat" at the catch point how to handle this ?

Do u mean you cannot access the variables openCat and readCat in your catch ?
Have you read local and global variables ?
anushikaroshan 3-Mar-15 0:25am    
Yes
[no name] 3-Mar-15 0:37am    
Then declare them globally. Like:
SqlCommand OpenCat = new SqlCommand(SQL, MainClass.Conn);
SqlDataReader readCat = new SqlDataReader();
try
{
readCat = OpenCat.ExecuteReader();
if (readCat.Read())
{
cmbPettyCashCode.Text = MainClass.SetDefo(readCat.GetValue(0).ToString(), "");
txtCatName.Text = MainClass.SetDefo(readCat.GetValue(1).ToString(), "");
readCat.Dispose();
}
else
{
readCat.Dispose();
}
}
catch (Exception error)
{
//access your variables here, as they are now global
cmbPettyCashCode.Text = "";
MessageBox.Show(error.Message.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally { }

best practice is using "USING BLOCK" as below, it will dispose the connection/command/ reader even when exception occurred
C#
using(SqlConnection connection = ...)
{
    connection.Open();
    ...
    using(SqlCommand command = ...)
    {
        using(SqlDataReader reader = command.ExecuteReader())
        {
            ... do your stuff ...
        } // reader is closed/disposed here
    } // command is closed/disposed here
} // connection is closed/disposed here


refer Understanding the 'using' statement in C#[^]
and SQLDataReader dispose?[^]
 
Share this answer
 
v2
You will need to declare your variables outside of the try block if you want to access them in the catch and/or finally block.

As @DamithSL already answered, use using statement to make sure your connection will be disposed at the end, whether an exception occurs or not.

You can still write your try/catch blocks within the using block and this way your catch block can focus on error handling: write exception details to log file and display error message to user.
 
Share this answer
 
SqlCommand OpenCat = new SqlCommand(SQL, MainClass.Conn);
SqlDataReader readCat = new SqlDataReader();
try
{
readCat = OpenCat.ExecuteReader();
if (readCat.Read())
{
cmbPettyCashCode.Text = MainClass.SetDefo(readCat.GetValue(0).ToString(), "");
txtCatName.Text = MainClass.SetDefo(readCat.GetValue(1).ToString(), "");
readCat.Dispose();
}
else
{
readCat.Dispose();
} 
}
catch (Exception error)
{
//access your variables here, as they are now global 
cmbPettyCashCode.Text = "";
MessageBox.Show(error.Message.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally { }
 
Share this answer
 
You have declared the OpenCat and ReadCat variables with in the try Scope, so you can't able to get the object out side the try block.

Declare both objects before try block starts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900