471,344 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

Getting error with IsDBNull and not sure why

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?

Jul 21 '05 #1
4 3170
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
Jul 21 '05 #2
BillG <Bi***@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
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
Jul 21 '05 #4
Cor Ligthert <no************@planet.nl> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by luna | last post: by
7 posts views Thread by James D. Marshall | last post: by
2 posts views Thread by James Wallace | last post: by
2 posts views Thread by Justin | last post: by
3 posts views Thread by Paul D. Fox | last post: by
2 posts views Thread by John Kotuby | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.