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