Easy question, IIF and Convert with DBNulls

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

Why doesn't this work:
cbAutoBill.Checked = IIf(IsDBNull(reader("autobill")), False,
Convert.ToBoolean(reader("autobill")))
Gives error: Object cannot be cast from DBNull to other types.

But this does:
cbAutoBill.Checked = IIf(IsDBNull(reader("autobill")), False,
reader("autobill"))

"autobill" is a bit

-Max
 
I could be wrong but I think that everything in the IIF statements gets
evaluated even if it is not used so that when reader("autobill") is null the
convert is evaluated and fails even though you don't want that value to be
used. I could be wrong but I think I've had it explained to me that way.
Have you tried this:

cbAutoBill.Checked = Convert.ToBoolean(IIf(IsDBNull(reader("autobill")),
False, reader("autobill")))

Paul Ross
Hebco, Inc.
 
Oh you know you're right. I totally get it now. IIF is just a function like
any other. It accepts 3 variables, which are *obviously* going to be
evaluated and THEN sent to the function! Duh!

-Max
 

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

Back
Top