Error on Dbnull and integers

T

tshad

I have a value coming from my Object (or it could also be from a
SqlDbReader) where I need to test for DBNull and 0.

I tried to do it in one call:
if (not (newPosition.ReportsTo is DBNull.Value)) andalso
(newPosition.ReportsTo <> 0) then

The first time I did it I used "and" and got the following error.

Operator is not valid for type 'DBNull' and type 'Integer'.

I then tried "andalso", as shown and still got the error.

I thought the whole point of using "andalso" was to stop checking if the
first test was true (which in this case it is).

If you look below I was just checking to see where I was getting the error
and I got it on the "0" test.
*************************************************************
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Operator is not valid for
type 'DBNull' and type 'Integer'.

Source Error:

Line 141:if (newPosition.ReportsTo is DBNull.Value) then
Line 142: trace.warn("ReportsTo is DBNull")
Line 143:end if
Line 144:if (newPosition.ReportsTo = 0) <-------
Line 145: trace.warn("newposition.ReportsTo = 0")
Line 146:end if
Line 147:if (not (newPosition.ReportsTo is DBNull.Value)) andalso
(newPosition.ReportsTo <> 0)then
*************************************************************

I can fix this by just checking the For DBNull first and then another test
of zero after that:
******************************************************************
if (not(newPosition.ReportsTo is DBNull.Value)) then
if (newPosition.ReportsTo <> 0) then

Do something

end if
end if

***************************************************************

But I would have thought that "andalso" would have worked for this.

Why doesn't it?

The way I would have thougtht this would check it is

if DBNull skip out of if section
then if = 0 skip out of if section

else do something

Thanks,

Tom
 
S

Stephany Young

Try using the IsDBNull method and I think you will find that it works a
treat.
 
T

tshad

Stephany Young said:
Try using the IsDBNull method and I think you will find that it works a
treat.

Tried that with the same result.

This is a better way to test for DBNull, in my example it worked correctly
on the test for DBNull.

The problem is trying to do 2 tests in the same if statement.

The error I am getting is on the (if newPosition.ReportsTo <> 0) test.

I understand why this is an error, and if the statement were:

if (not (newPosition.ReportsTo is DBNull.Value)) AND (newPosition.ReportsTo
<> 0)then

then I would understand - as both tests get evaluated regardless as to what
the first test result is.

But if it is:

if (not (newPosition.ReportsTo is DBNull.Value)) ANDALSO
(newPosition.ReportsTo <> 0)then

I was under the impression that the 2nd expression would not be evaluated if
the first test was false - which it is here. newPosition.ReportsTo is NULL.
So the second test shouldn't even happen. If this is not the case, what is
the point of using ANDALSO in place of AND?

Thanks,

Tom
 
A

Armin Zingler

tshad said:
I was under the impression that the 2nd expression would not be
evaluated if the first test was false - which it is here.
newPosition.ReportsTo is NULL. So the second test shouldn't even
happen. If this is not the case, what is the point of using ANDALSO
in place of AND?

The point is exactly solving the problem you described. AndAlso does not
evaluate the second expression if the first is False. And is a binary
operator the always combines both values. (See also: Docs for And and
AndAlso)


Armin
 
T

tshad

Armin Zingler said:
The point is exactly solving the problem you described. AndAlso does not
evaluate the second expression if the first is False. And is a binary
operator the always combines both values. (See also: Docs for And and
AndAlso)

You're right.

That was what was happening.

But if you have this:

if (not IsDBNull(newPosition.ReportsTo)) ANDALSO (newPosition.ReportsTo <>
0)then

This works fine as does:

if not IsDBNull(newPosition.ReportsTo) ANDALSO (newPosition.ReportsTo <>
0)then

So I assume the "not" gets evaluated before the ANDALSO. I would normally
use the parens around the "not IsDBNull(NewPosition.ReportsTo)" to be clear.

I was curious if the the 2 tests (the Null and 0 tests) would be evaluated
before the not, but this is not the case, apparently.

Thanks,

Tom
 

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

Similar Threads


Top