I’m trying to handle DBNull exception while reading data from database.
It’s my code:
...
Dim SQLRDAs SqlDataReader
...
val1= GetStringFromDB(Trim(SQLRD("Name")))
val2= GetStringFromDB(Trim(SQLRD("Level")))
val2= GetStringFromDB(Trim(SQLRD("blahblah")))
...
Public Function GetStringFromDB(ByVal inputValue) As String
Dim outputValue As String
If IsDBNull(inputValue) Then
outputValue = "null"
Else
outputValue = inputValue
End If
Return outputValue
End Function
But still I get Conversion from type 'DBNull' to type 'String' is not valid. error. What’s wrong?
IsDBNull checks if a value is missing. It is not equivalent to Nothing or to String.Empty.
Are you sure that your inputValue contains DBNull.Value?
I will change your code in this way:
Also note that you don’t specify the type of inputValue, in my answer I assume As String