Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange warning

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
Here's the code:
Code:
'====================================================================================================================================
Private Function Test_SQL_Connection() As Boolean
'===================================================================================================================================
' Purpose       : Tests the connection to the SQL Server.
' Description   : Uses global memvar gcConnectStrDB to connect to the specified DB Server and open the specified database. 
' Parameters    : None.
' Returns       : Success as Boolean.
' Side effects  : None.
' Notes         : 1. Application-specific.
'                 2. Verbose on errors, silent otherwise.
' Author        : Ilya I. Rabyy
' Revisions     : 2020-07-30 by Ilya – started 1st draft.
'===================================================================================================================================
Dim llRet As Boolean = True ' Initially
Dim lcErrMsg As String = "", loSqlConnection As SqlConnection, loErr As Exception

Try
   loSqlConnection = New SqlConnection(gcConnectStrDB)
Catch loErr
   llRet = False
   gcProcLog = gcProcLog & Now.ToString("yyyy-MM-dd HH:mm:ss") & ": ERROR OCCURED!" & vbCrLf & Space(21) & loErr.Message & _
               vbCrLf & "Occured in " & vbCrLf & loErr.Source & vbCrLf & loErr.StackTrace & vbCrLf & Space(21) & _
               "Program quits." & vbCrLf
   gcErrStr = "Error occurred when testing SQL Server Connection." & "See details in the " & gcLogFile & vbCrLf & _
               "Program quits."
   gcProcLog = gcProcLog & Space(21) & "Program quits." & vbCrLf
   File.WriteAllText(gcLogFile, gcProcLog) ' There's not much text in the Process Log string yet, write it all anyway
   MsgBox(gcErrStr, MsgBoxStyle.Critical, gcAppName)
End Try

If Not llRet Then
   Return llRet
End If

gcProcLog = gcProcLog & Now.ToString("yyyy-MM-dd HH:mm:ss") & ": Sql Connection tested successfully." & vbCrLf

loSqlConnection.Close()

Return llRet
End Function
And here's what's happening when I tried to Save this module:
20200803_SqlConnection_Close_Strange_Warning_cjvaik.jpg


Could you explain to me why the System doesn't see that loSqlConnection was 1. Declared, 2. Assigned a value of a proper type in the TRY-CATCH-EMDTRY construct?
And how do I fix this glitch?
TIA!

Regards,

Ilya
 
There's a route through your code that can end up with loSQlConnection not being assigned a value - if your Try statement fails. hence the warning

One fix would simply be to add

[tt]loSqlConnection = New SqlConnection()[/tt]

before the Try block
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top