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
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