4

I am working in VB.NET and I am wondering about the difference between Nothing and System.DBNull.

When I fire save query at that time I am giving value from grid at runtime like as follow:

gvMain.Rows(j).Cells("Brand").Value.ToString()

But it shows me error when it has value of Nothing and it works perfectlly when it has value of System.DBnull.

What to do in this case?
Thanks in advance

SysDragon
  • 9,692
  • 15
  • 60
  • 89
Jay Tankariya
  • 69
  • 3
  • 12
  • check http://stackoverflow.com/questions/164697/net-dbnull-vs-nothing-across-all-variable-types – spajce Jan 30 '13 at 11:08

2 Answers2

8

The keyword Nothing is used to specify or asign that a var of reference type is not pointing anything, no object is instanciated for this var.

DBNull.Value, on the other hand, is an object used to point out that a type of a field of the DataBase is of null value.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
4

Nothing is of Type System.Object.

It represents the default value of any data type.

DBNull.Value is of Type System.DBNull

If something shows up as System.DBNull, that means that even though it doesn't have a value, it has a valid pointer. As you may have found out, it cannot be converted to a string, integer, etc. You must do a check (preferably using IsDBNull.

If IsDBNull(gvMain.Rows(j).Cells("Brand").Value) Then
    Return String.Empty
Else
    Return gvMain.Rows(j).Cells("Brand").Value.ToString().Trim()
End If
Donald.Record
  • 301
  • 3
  • 7