我有以下 ASP.NET (VB) 代码:
strLocation = CStr(q1("LocationName")) + " " + CStr(q1("LocationAddress")) + " " + CStr(q1("LocationCity"))
由于 LocationCity 为空:
我得到从类型“DBNull”到类型“String”的转换无效。
有没有办法解决这个问题。
如果只是 LocationCity 我可能会这样做:
If IsDBNull(q1("LocationCity")) Then
strLocation = ""
Else
strLocation = CStr(q1("LocationCity"))
End If
我也尝试过:
strLocation = If(CStr(q1("LocationName")), "") + " " + If(CStr(q1("LocationAddress")), "") + " " + If(CStr(q1("LocationCity")), "")
但得到了相同的结果
在 C# 中我通常会使用 ??但不确定 ASP.NET VB 中的最佳方法
最佳答案
相当于 VB.NET 中的 C# ??
是 IF Operator
strLocation = If(IsDBNull(q1("LocationCity")), "", CStr(q1("LocationCity")))
请勿使用 IIF Function 因为它已被弃用,不支持短路并且类型不安全。
关于ASP.NET VB - 从类型 'DBNull' 到类型 'String' 的转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830990/