1

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

2

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

else 

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

end if

IIF will evaluate failed part also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.