Johns Yong
Johns Yong

Reputation: 77

How to create a condition to check if the datatable item is null in vb

Now I want if the dt.rows(i).item(0) is null, then some code..

This is my code:

If dtpay.Rows(i).Item(23).ToString Is Nothing Then
  GoTo finalline
End If

But seems like the code is not working.. Thanks a lot for your concern :D

Upvotes: 3

Views: 15512

Answers (5)

Tim Schmelter
Tim Schmelter

Reputation: 460048

You should use DataRow.IsNull

If dtpay.Rows(i).IsNull(23) Then
  ' ..... '
End If

If it's a value type(like Integer) you can also use the DataRow-extension method Field:

Dim myNullableField As Int32? = dtpay.Rows(i).Field(Of Int32?)
If Not myNullableField.HasValue Then
  ' you get it's value via myNullableField.Value '
End If

Upvotes: 3

Johns Yong
Johns Yong

Reputation: 77

If dt.Rows(i).Item(23) Is Nothing OrElse dt.Rows(i).Item(23).ToString = "" OrElse dt.Rows(i).Item(23) = vbNull Then GoTo finalline End If

Upvotes: 0

Beldi Anouar
Beldi Anouar

Reputation: 2180

Try just :

If dtpay.Rows(i).Item(23) Is Nothing Then
  GoTo finalline
End If

Upvotes: 0

Veeke
Veeke

Reputation: 239

Nothing is not the same as Null. Nothing returns the default value for the type of the field (0 for numbers, "" for text,...)

    If IsDBNull(dtpay.Rows(i).Item(23)) Then
        GoTo finalline
    End If

Upvotes: 2

Ian
Ian

Reputation: 30813

You can use GetType() to check if an object is DBNull in VB.Net:

If dtpay.Rows(i).Item(23).GetType() Is GetType(DBNull) Then
    'Do something
End If

That being said, the DBNull in your code above may also happen in the dtpay.Rows(i). Thus, check also where the DBNull occurs.

Upvotes: 3

Related Questions