Dim substr As String = dr.Item(4)
if substr is {System.DBNull}
substr = ""
if substr is "" then
do not do substr = substr.Substring(0, 2)
else
substr = substr.Substring(0, 2)
I need this in vb.net
Dim substr As String = dr.Item(4)
if substr is {System.DBNull}
substr = ""
if substr is "" then
do not do substr = substr.Substring(0, 2)
else
substr = substr.Substring(0, 2)I need this in vb.net
I'll give it a shot:
Depending on wether or not the value is coming from a DB call (DB.Null.Value) or from a naked object (= Nothing), try this:
'FROM DATABASE CALL:
if dr.Count >= 4 Then 'Try not to raise 'Index Out Of Range'
Dim substr As String = Cstr(dr.Item(4)) 'Option Strict Compl.
if substr = System.DBNull.Value Then
substr = ""
Else
substr = substr.Substring(0, 2)
End If
End If
'FROM OBJECT:
if dr.Count >= 4 Then 'Try not to raise 'Index Out Of Range'
Dim substr As String = Cstr(dr.Item(4)) 'Option Strict Compl.
if not IsNothing substr
substr = ""
Else
substr = substr.Substring(0, 2)
End If
Hope I'm in the ballpark.
Sorry troupm that does not work, you cannot compare your value to system.DBNull.value, and you cannot compare it to nothing because it is an actual value (system.DBNULL).
You need to use the isDBNull() method:
if isDBNull(dr.Item(4).value) then
substr = ""
else
substr = dr.Item(4).value.ToString.Substring(0, 2)
end if
Also you are assigning the Column value directly to the string variable
in the first line:
Dim substr As String = dr.Item(4)
This will cause an Exception if the value is system.DBNull, so you must check for DBNull first using the isDBNull() method before trying to assign this value to a string.
I know is an old thread, but it came up first in Google, so it might as well have the Correct Answer, and save someone else some time.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.