DbNull

  • Thread starter Thread starter Jon Cosby
  • Start date Start date
J

Jon Cosby

Which is it? I have a scalar operation with a sql command, and I want to
check for null values. If I check for dbnull it reads the command as a
string. If I set it to a string, it reads it as dbnull.



Dim dcOrdNumber As SqlCommand
Dim null As DBNull

If dcOrdNumber.ExecuteScalar() Is null Then ...

' Cast from type 'DBNull' to type 'String' is not valid.


If dcOrdNumber.ExecuteScalar() = "NULL" Then ...

' Operator is not valid for type 'DBNull' and string "NULL".
 
Maybe you can use one of these two options:

If Microsoft.VisualBasic.IsDBNull(dcOrdNumber.ExecuteScalar()) Then ...

If dcOrdNumber.ExecuteScalar() Is System.DBNull.Value Then ...

Hope it helps ...
 
ExecuteScalar return a System.DBNull value which isn't NULL or null

try:
If dcOrdNumber.ExecuteScalar() <> System.DBNull Then ...

I use C# .... but is work in Vb also.

GoodLuck
Lorenzo Soncini
 
Make that "Is DBNull" not "Is null". Two different things. "null" is the
"value" of any uninitialized object. But if a database field is null, it's
assigned the singleton instance DBNull.Value, so you have to check that it's
of type DBNull.

--Bob
 
Back
Top