469,613 Members | 1,208 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Error on Dbnull and integers

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
Nov 27 '05 #1
6 4433
Try using the IsDBNull method and I think you will find that it works a
treat.
"tshad" <tf*@dslextreme.com> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
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

Nov 27 '05 #2

"Stephany Young" <noone@localhost> wrote in message
news:eY**************@tk2msftngp13.phx.gbl...
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


"tshad" <tf*@dslextreme.com> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
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


Nov 27 '05 #3
"tshad" <tf*@dslextreme.com> schrieb
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

Nov 27 '05 #4
"Armin Zingler" <az*******@freenet.de> schrieb
And is a binary operator the always combines both values.


....*that* always combines...
Armin
Nov 27 '05 #5
"Armin Zingler" <az*******@freenet.de> wrote in message
news:O9**************@TK2MSFTNGP14.phx.gbl...
"tshad" <tf*@dslextreme.com> schrieb
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)


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

Armin

Nov 27 '05 #6
"tshad" <tf*@dslextreme.com> schrieb
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.


Operator precedence:
http://msdn.microsoft.com/library/en...Precedence.asp
Armin

Nov 27 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Filipe Cristóvão | last post: by
5 posts views Thread by David Lozzi | last post: by
1 post views Thread by Child | last post: by
1 post views Thread by fredda054 | last post: by
8 posts views Thread by wink martindale | last post: by
3 posts views Thread by Doug Durrett | last post: by
3 posts views Thread by JFB | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.