SqlConnection cnn; try { cnn = new SqlConnection("Some connection string here"); cnn.Open(); } catch(Exception ex) { if (cnn != null) MessageBox.Show(cnn.State.ToString()); } finally { if (cnn != null) cnn.Close(); }The one problem in previous code is that it will not compile, because it uses (only in Catch - it will be executed before Finally, so you get only one error and no two) cnn for comparison (cnn != null) before it is "initialized" (in C# compiler's terminology - there is no assignment to this variable before it is used for the first time). This is because compiler assumes that code execution can run into Catch block also if first row in Try block fails and in this case cnn will remain unassigned. So changing declaration to:
SqlConnection cnn = null;
will prevent this. I called this "initialization", because:cnn = new SqlConnection("Some connection string here");i call "instancing".
Dim cnn As SqlConnection Dim cnn As SqlConnection = Nothing(Nothing is equivalent to C#'s null). This is the reason for successful compiling of previous code (translated to VB .NET) without errors and without "initializing" of cnn to Nothing.
Plamen Ivanov
MCSD .NET Early Achiever and MCAD .NET Charter Member (VB .NET/SQL Server 2000)
MCSD (VB 6.0/SQL Server 2000) br>
VB (.NET) - what other language do you need in the whole Universe?...