Getting error with IsDBNull and not sure why

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following line in my code

paid.MonthsPaid = IIf(IsDBNull(Row("MonthsPaid")),
Integer.MinValue, Convert.ToInt16(Row("MonthsPaid")))

and I get the error message

Object cannot be cast from DBNull to other types.

What am I doing wrong?
 
BillG,

Avoid using the IIF. I have seen more that there is a kind of special logic
in it.
As well do not use the Short(int16) when it is not really needed, the
Integer(Int32) is in VBNet the default.

Why not just use
\\\
If Row("MonthsPaid") Is DBNull.value then
paid.MonthsPaid = 0
Else
paid.MonthsPaid = Cint(Row("MonthsPaid")
End if
///

I hope this helps,

Cor
 
BillG said:
I have the following line in my code

paid.MonthsPaid = IIf(IsDBNull(Row("MonthsPaid")),
Integer.MinValue, Convert.ToInt16(Row("MonthsPaid")))

and I get the error message

Object cannot be cast from DBNull to other types.

What am I doing wrong?

From the docs for Iif:

<quote>
Note The expressions in the argument list can include function calls.
As part of preparing the argument list for the call to IIf, the Visual
Basic compiler calls every function in every expression. This means
that you cannot rely on a particular function not being called if the
other argument is selected by Expression.
</quote>

To my mind, that makes it a lot less useful than the normal ternary
operator of languages such as C/C++, Java and C#.
 
Jon,
To my mind, that makes it a lot less useful than the normal ternary
operator of languages such as C/C++, Java and C#.

And JavaScript where it is in my opinon often used.

You sentence is in my opinion "true", maybe can you add that to your page
and than directly change that awful "With" part and look at that IDE part as
you promished.

(As an advantage from C# of course)

:-)

Cor
 
Cor Ligthert said:
And JavaScript where it is in my opinon often used.

You sentence is in my opinion "true", maybe can you add that to your page
and than directly change that awful "With" part and look at that IDE part as
you promished.

I still don't think the With part is awful (clearly many VB.NET fans
feel it is an advantage as they ask why it isn't in C#, and clearly
many C# fans feel it's not an advantage as they say they're glad it
isn't in there), but I'll remove it seeing as it seems to rub you up
the wrong way so much. I'll try to find some time to test the large
VB.NET solution business. Unfortunately my time is *very* limited at
the moment...
(As an advantage from C# of course)

Thing is, it's not that much of an advantage - not significant by any
stretch of the imagination, as just using a multi-line select fixes the
problem, as has already been mentioned.
 
Back
Top