DbNull

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".
 
A

anouk

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 ...
 
L

Lorenzo Soncini

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
 
B

Bob Grommes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top