simplified null check

  • Thread starter Thread starter Smokey Grindle
  • Start date Start date
S

Smokey Grindle

I have a sproc that returns null, true or false, and I have the following
code

If cmd.ExecuteScalar Is DBNull.Value Then

Return False

Else

Return CBool(cmd.ExecuteScalar)

End If



is there any way to simplify this so i dont have to execute the command
twice? i thought maybe sqlboolean type could help but i can directly cast
the scalar to that type it seems.. anyone have any ideas?
 
Dim myObj as Object

obj = cmd.ExecuteScalar()

If IsDBNull(obj) Then
Return False
Else
Return CBool(myObj)
End If

Your ExecuteScalar needs to return a boolean, or else the CBool will fail at
run time.
 
thanks a lot for the help!


Marina Levit said:
Dim myObj as Object

obj = cmd.ExecuteScalar()

If IsDBNull(obj) Then
Return False
Else
Return CBool(myObj)
End If

Your ExecuteScalar needs to return a boolean, or else the CBool will fail
at run time.
 
Dim myObj as Object

obj = cmd.ExecuteScalar()

If IsDBNull(obj) Then
Return False
Else
Return CBool(myObj)
End If

Your ExecuteScalar needs to return a boolean, or else the CBool will fail at
run time.

Even simpler version or your excellent sample.

If cmd.ExecuteScalar Is DBNull.Value Then
Return False
Return CBool(cmd.ExecuteScalar)

or this way which executes the command only once.

object test = false;

if (((test = cmd.ExecuteScalar()) == DBNull.Value) || ((bool)test == false))
return false;
return true;

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Back
Top