Nk SP
Nk SP

Reputation: 862

VB Net IsDBNull issue

I have an Issue with the IsDBNull method. This is my code:

   Dim a as Bool=  IIf(IsDBNull(reader("field")), _
                       False, _
                       Convert.ToBoolean(reader("field")))

The code above should return False if the column is false but I get the exception:

Impossible to cast DBNull to other types.

The Convert.ToBoolean fails

Upvotes: 1

Views: 389

Answers (2)

Anil
Anil

Reputation: 3752

If(IsDBNull(reader("field"))) then 
return False

else 

Convert.ToBoolean(reader("field")))

end if

IIF will evaluate failed part also.

Upvotes: 1

Richard
Richard

Reputation: 109100

All arguments of Iif are evaluated when called, whether they'll be used or not.

In this respect it is not equivalent to C#'s conditional operator (which only evaluates the boolean control sub-expression and one of the other two sub-exptressions).

You need to use VB.Net's If operator which does do lazy evaluation.

Upvotes: 2

Related Questions