Why does the following code:
为什么以下代码:
A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
results in the following error:
导致以下错误:
Conversion from type 'DBNull' to type 'String' is not valid.
When AndAlso is supposed to short-circuit according to this article:
当AndAlso根据这篇文章被认为是短路的时候:
http://support.microsoft.com/kb/817250
http://support.microsoft.com/kb/817250
5 个解决方案
#1
9
You are correct. AndAlso
is short circuiting.
你是对的。而且也是短路的。
However, the error comes by calling CurRow("GuyBook")
(verify this in a debugger to make sure I'm not a liar or making some crazy assumptions or just misremembering* ;-). Before you ask for a value, you need to ask the DataRow if it has a value. That is, use:
然而,错误来自调用CurRow(“GuyBook”)(在调试器中验证这一点,以确保我不是骗子或做出一些疯狂的假设或只是误报* ;-)。在询问值之前,您需要询问DataRow是否有值。也就是说,使用:
CurRow.IsNull("BuyBook")
Happy coding.
快乐的编码。
*One should just be able to compare with DBNull.Value
or use IsDBNull
. However, I am fairly certain that I ran into a row before that threw these exceptions instead of returning a DBNull
object. Start by finding out -- in the Immediate Window of the Debugger -- exactly which expression throws the exception.
*应该只能与DBNull.Value进行比较或使用IsDBNull。但是,我很确定在抛出这些异常而不是返回DBNull对象之前遇到了一行。首先找出 - 在调试器的立即窗口中 - 确切地说哪个表达式抛出了异常。
#2
4
Have you tried comparing like this:
您是否尝试过这样比较:
If CurRow("BuyBook") Is DBNull.Value Then
'...
End If
#3
0
Instead of not IsDBNull(CurRow("BuyBook")), use NOT (CurRow("BuyBook")) is System.Dbnull.Value). Try this:
而不是IsDBNull(CurRow(“BuyBook”)),使用NOT(CurRow(“BuyBook”))是System.Dbnull.Value)。尝试这个:
A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
OR
要么
A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
#4
0
If dt.Rows(0)("BuyBook") = DBNull.Value Then
End If
Hope it helps.
希望能帮助到你。
#5
-1
In vb.net I usually do something like this:
在vb.net中,我通常会这样做:
A = (CurRow("BuyBook") & "" = "Yes")
Old trick from vb6 era. If CurRow("BuyBook")
is null, vb.net will consider it as empty string when concatenating it with another string.
vb6时代的老把戏。如果CurRow(“BuyBook”)为null,则vb.net在将其与另一个字符串连接时会将其视为空字符串。
#1
9
You are correct. AndAlso
is short circuiting.
你是对的。而且也是短路的。
However, the error comes by calling CurRow("GuyBook")
(verify this in a debugger to make sure I'm not a liar or making some crazy assumptions or just misremembering* ;-). Before you ask for a value, you need to ask the DataRow if it has a value. That is, use:
然而,错误来自调用CurRow(“GuyBook”)(在调试器中验证这一点,以确保我不是骗子或做出一些疯狂的假设或只是误报* ;-)。在询问值之前,您需要询问DataRow是否有值。也就是说,使用:
CurRow.IsNull("BuyBook")
Happy coding.
快乐的编码。
*One should just be able to compare with DBNull.Value
or use IsDBNull
. However, I am fairly certain that I ran into a row before that threw these exceptions instead of returning a DBNull
object. Start by finding out -- in the Immediate Window of the Debugger -- exactly which expression throws the exception.
*应该只能与DBNull.Value进行比较或使用IsDBNull。但是,我很确定在抛出这些异常而不是返回DBNull对象之前遇到了一行。首先找出 - 在调试器的立即窗口中 - 确切地说哪个表达式抛出了异常。
#2
4
Have you tried comparing like this:
您是否尝试过这样比较:
If CurRow("BuyBook") Is DBNull.Value Then
'...
End If
#3
0
Instead of not IsDBNull(CurRow("BuyBook")), use NOT (CurRow("BuyBook")) is System.Dbnull.Value). Try this:
而不是IsDBNull(CurRow(“BuyBook”)),使用NOT(CurRow(“BuyBook”))是System.Dbnull.Value)。尝试这个:
A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
OR
要么
A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
#4
0
If dt.Rows(0)("BuyBook") = DBNull.Value Then
End If
Hope it helps.
希望能帮助到你。
#5
-1
In vb.net I usually do something like this:
在vb.net中,我通常会这样做:
A = (CurRow("BuyBook") & "" = "Yes")
Old trick from vb6 era. If CurRow("BuyBook")
is null, vb.net will consider it as empty string when concatenating it with another string.
vb6时代的老把戏。如果CurRow(“BuyBook”)为null,则vb.net在将其与另一个字符串连接时会将其视为空字符串。