为什么以下代码会:
A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
导致以下错误:
Conversion from type 'DBNull' to type 'String' is not valid.
根据这篇文章,当 AndAlso 应该进行短路时:
为什么以下代码会:
A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
导致以下错误:
Conversion from type 'DBNull' to type 'String' is not valid.
根据这篇文章,当 AndAlso 应该进行短路时:
AndAlso
是短路的。CurRow("GuyBook")
(在调试器中验证以确保我不是说谎或做出一些疯狂的假设或只是记错了 *;-)。在请求值之前,您需要询问DataRow 它是否有一个值。也就是说,请使用:CurRow.IsNull("BuyBook")
愉快的编程。
应该只需与DBNull.Value
进行比较或使用IsDBNull
。但我相当确定之前遇到过一行代码,它抛出了这些异常而不是返回一个DBNull
对象。首先,在调试器的即时窗口中找出确切地引发异常的哪个表达式。
你尝试过像这样进行比较吗:
If CurRow("BuyBook") Is DBNull.Value Then
'...
End If
不要使用 IsDBNull(CurRow("BuyBook")),而是使用 NOT (CurRow("BuyBook") is System.DBNull.Value)。试试这个:
A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
或者
A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
If dt.Rows(0)("BuyBook") = DBNull.Value Then
End If
希望能有所帮助。
A = (CurRow("BuyBook") & "" = "Yes")
这是VB6时代的老技巧。如果CurRow("BuyBook")
为空,vb.net在将其与另一个字符串连接时会将其视为空字符串。
CurRow("BuyBook").ToString = "Yes"
。 - jmoreno
A = (not IsDBNull(CurRow("BuyBook"))) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
- Amar Palsapure