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

Re: DbNull by anouk

anouk
Sat Jul 26 10:30:17 CDT 2003

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

"Jon Cosby" <jcosby@mindspring.com> wrote in message
news:bfu6jb$cm7$1@slb9.atl.mindspring.net...
> 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".
>
>
>
>



Re: DbNull by Lorenzo

Lorenzo
Sat Jul 26 10:54:02 CDT 2003

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

"Jon Cosby" <jcosby@mindspring.com> ha scritto nel messaggio
news:bfu6jb$cm7$1@slb9.atl.mindspring.net...
> 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".
>
>
>
>



Re: DbNull by Bob

Bob
Sat Jul 26 12:30:29 CDT 2003

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

"Jon Cosby" <jcosby@mindspring.com> wrote in message
news:bfu6jb$cm7$1@slb9.atl.mindspring.net...
> 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".
>
>
>
>



Re: DbNull by Michael

Michael
Sat Jul 26 20:54:28 CDT 2003

if (dcOrdNumber.ExecuteScalar().GetType() is Type.GetType("System.DBNull"))



"Jon Cosby" <jcosby@mindspring.com> wrote in message
news:bfu6jb$cm7$1@slb9.atl.mindspring.net...
> 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".
>
>
>
>