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

Dani AI

Generated

A few practical points and a compact, safe pattern to take the first two characters without throwing when the column is DB null or shorter than two chars.

Both and were on the right track (check for DB null before assigning to a String), but comparing a casted String to System.DBNull.Value or assigning directly before checking can raise exceptions. Prefer an approach that treats the raw field as an object that may be DBNull, then produce a safe string and guard the substring length.

Example (uses DataRow.Field(Of T) which returns Nothing for DB null and keeps the code concise):

' safe, compact pattern
Dim raw As String = dr.Field(Of String)(4)          ' or use the column name: dr.Field(Of String)("ColumnName")
Dim substr As String = If(String.IsNullOrEmpty(raw), String.Empty, raw.Substring(0, Math.Min(2, raw.Length)))

Notes and alternatives:

  • If DataRow.Field(Of T) is not available in your project, use DataRow.IsNull(index) or Convert.IsDBNull to check before converting/assigning.
  • Always prefer a column name over a numeric index when possible (less fragile if columns move).
  • Turn on Option Strict to catch accidental late casts; Field(Of String) helps keep strong typing.
  • Guard Substring with Math.Min to avoid ArgumentOutOfRange when the source string is shorter than two characters.

Quick troubleshooting:

  • If you still see exceptions, confirm dr is not Nothing and the row actually contains the column (IndexOutOfRange).
  • If the source is a DataReader instead of DataRow, use the reader's IsDBNull/ GetString/GetOrdinal patterns before calling Substring.
  • Trim input if you want to treat whitespace-only values as empty.

This pattern is robust, readable, and avoids the pitfalls in earlier replies while keeping the intent—"get the first two chars if present"—straightforward.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.