Jun01
Conversion from type ‘DBNull’ to type ‘String’ is not valid
on June 1st, 2009 at 1:51 amIn a VB.net application, retrieving variables from a database, if the value was null, it would cause the error, “Conversion from type ‘DBNull’ to type ‘String’ is not valid”. A work-around to stop this error was to create a function that would evaluate the imported database variable for a null value as an object then convert it to a string for use without error in your code.
Here is the VB.net code:
Function checkNull(Byval variable as object) as String
If IsDBNull(varialbe) Then
Return("")
Else
Return (variable)
End If
End Function
Hi, cool post. I have been wondering about this topic,so thanks for writing.
In it something is. Now all became clear, many thanks for the help in this question.
Hi there,
Very nice example,i am struggling to convert dbnull to string.Every time i am getting error.I solved my problem with your example.
Thanks a lot
Hi,
We can also check below
VB.NET
String nullString = If(IsDBNull(nullString)), String.Empty, nullString)
C#
String nullString = IsDBNull(nullString) ? String.Empty: nullString