Arul Posted February 26, 2008 Posted February 26, 2008 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(); } } Quote
Administrators PlausiblyDamp Posted February 26, 2008 Administrators Posted February 26, 2008 You need to move the declaration of MyDBConn to before the try block. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arul Posted February 27, 2008 Author Posted February 27, 2008 Thanks a lot... I corrected and now its working Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.