2

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

Ian
  • 30,182
  • 19
  • 69
  • 107
Johns Yong
  • 57
  • 1
  • 2
  • 7

5 Answers5

3

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • This code still not working... Actually i want to skip all the code below when the item(23) is null.. Do you have any way other than this solution? Im sorry and thank you for you help – Johns Yong May 13 '16 at 07:40
  • In what sense does it not working? Does it go to the `If` block? – Ian May 13 '16 at 07:41
  • I run this on try block.. Anyway i dont set the GoTo to outside of try block – Johns Yong May 13 '16 at 07:44
  • The first thing you may need to check is if it is actually entering the `If` block - not the `try` block. Because it shouldn't go out of the `try` block if there is no `exception` – Ian May 13 '16 at 07:48
  • @JohnsYong does any of the answer help you? If it does, please do not forget to accept (tick V in the top left of the answer) and/or upvote it (press up-arrow in the top left of the answer). You could accept only *one* answer, but you could *upvote* as many answers as you want. – Ian May 26 '16 at 05:14
  • The answers from Ian, Veeke, and Tim Schmelter all work. – phrebh Aug 19 '22 at 15:37
2

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
Veeke
  • 229
  • 1
  • 6
2

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This code also still not working... Actually i want to skip all the code below when the item(23) is null.. Do you have any way other than this solution? Thanks in advance – Johns Yong May 13 '16 at 07:42
  • 1
    @JohnsYong: your code does the opposite, if it is `DnBull` you will enter the code. If you don't want that: `If Not dtpay.Rows(i).IsNull(23) Then` – Tim Schmelter May 13 '16 at 07:57
  • I just got the solution. Thank you for your help.. I dont know why but this code is working. Anyway, thank you very much for your help! Im using this kind of code: 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 – Johns Yong May 13 '16 at 08:13
  • The answers from Ian, Veeke, and Tim Schmelter all work. – phrebh Aug 19 '22 at 15:38
0

Try just :

If dtpay.Rows(i).Item(23) Is Nothing Then
  GoTo finalline
End If
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
  • if you want to skip all the code below when the item(23) is null : try this line If dtpay.Rows(i).Item(23) Is Nothing Then exit sub Or If IsDBNull((dtpay.Rows(i).Item(23)) Then exit sub – Beldi Anouar May 13 '16 at 08:03
0

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

Johns Yong
  • 57
  • 1
  • 2
  • 7