Difference between nothing and system.DBNull

I am working in VB.NET and I am curious about the difference between Nothing

and System.DBNull

.

When I run the save request at that time, I give the value from the grid at runtime as shown below:

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

      

But it shows me an error when it matters Nothing

, and it works great when it matters System.DBNull

.

What to do in this case?
thanks in advance

+3


source to share


2 answers


The keyword is Nothing

used to indicate or assign that a variable of a reference type specifies nothing, no object is created for this var.



DBNull.Value

, on the other hand, is an object indicating that the type of a database field is null.

+7


source


Nothing

is of type System.Object.

It represents the default for any data type.

DBNull.Value

is of type System.DBNull



If something is displayed as System.DBNull

, it means that even if it doesn't matter, it has a valid pointer. As you may have found out, it cannot be converted to a string, integer, etc. You should 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

      

+2


source







All Articles