How do you cast a 'null' value into a 'string'? i cant find ANY info on that. If it's not possible, what a way around this when a databse field has a null value in it?
Printable View
How do you cast a 'null' value into a 'string'? i cant find ANY info on that. If it's not possible, what a way around this when a databse field has a null value in it?
VB Code:
Dim myString As String If IsDBNull(myRow.Item("Example")) = True Then myString = "" Else myString = CStr(myRow.Item("Example")).TrimEnd End If
OR:
Code:Dim myString As String
If myRow.Item("Example") Is DBNull.Value Then
myString = ""
Else
myString = CStr(myRow.Item("Example")).TrimEnd
End If
neither one worked. It's coming from a database table and I get this message:
cast from type 'DBNull' to type 'String' is not valid
I thought for sure that code y'all had would work.
I used this for a quick project
VB Code:
Private Function DBReturn(ByVal objValue As Object) As Object If TypeOf (objValue) Is DBNull Then Return Nothing Else Return objValue End If End Function
and then
VB Code:
'cast The object type returned by DBReturn to the proper Data type 'so... Msgbox (CStr(DBReturn(myRow.Item("Example"))).TrimEnd)
I am sure that someone will post a more elegant solution but this works..
Being that a textbox should have nothing for text when the form loads, a simple:
VB Code:
If Not(myrow.Item(0)) Is DbNull.Value Then myTextbox.Text = myrow.item(0)