View Single Post
 
Old 05-26-2016, 07:31 AM
Maany Maany is offline
Newcomer
 
Join Date: Nov 2014
Posts: 9
Angry variable loses its value once exiting the if statement

hi all,

this may sound stupid and silly but it is driving me crazy, in below code C_Code= maintain its value returned from the sql database till the line in red below, it becomes "", and it gives a warning saying :"Variable 'C_Code' is used before it has been assigned a value, a null reference exception could result at runtime".

why ?!?!?

Code:
Public Function Get_Code(ByRef Country_Name As String) As String
        
        Dim C_Code As String
		Rs.Open("Select  * from Countries where Country like '" & Country_Name & "'", CN)
		If Not Rs.EOF Then
                        C_Code = Rs.Fields("Code").Value
		End If
		Rs.Close()
        CN.Close()
        Get_Code = C_Code
	End Function
Reply With Quote