Why does setting a DataSet row value to Nothing initialize it to the data type minvalue?
Question
When setting a DataSet row column to Nothing
, like row.Date = Nothing
, why does it initialize it to the data type minvalue? In this case a date, that gets set to 0000-01-01
. The column is set to allow null etc., and if I don't set the row to anything at all, it will leave the column empty. So why does Nothing
act this way?
In C# I would've set it to DbNull
, I guess, but I'm a tad green on VB.NET - as you might be able to tell. :)
Solution
Hps is correct that you should use DBNull.Value
to assign a NULL
value to a database column.
row.Date = DBNull.Value
The reason you see the a default value being set is that the keyword Nothing
in VB.NET is comparable to default(T)
in C#, not C#'s null
keyword.
OTHER TIPS
I think you will be able to set DBNull in VB.Net as well something like
table.Rows(0)(0) = System.DBNull.Value
or row.Date = System.DBNull.Value
For assigning the "Nothing", you will need to have Nullable Type