我有以下ASP.NET(VB)代码:
strLocation = CStr(q1("LocationName")) + " " + CStr(q1("LocationAddress")) + " " + CStr(q1("LocationCity"))
Run Code Online (Sandbox Code Playgroud)
由于LocationCity为null:
我从类型'DBNull'到类型'String'的转换无效.
有没有办法来解决这个问题.
如果它只是LocationCity,我可能会做类似的事情:
If IsDBNull(q1("LocationCity")) Then
strLocation = ""
Else
strLocation = CStr(q1("LocationCity"))
End If
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
strLocation = If(CStr(q1("LocationName")), "") + " " + If(CStr(q1("LocationAddress")), "") + " " + If(CStr(q1("LocationCity")), "")
Run Code Online (Sandbox Code Playgroud)
但得到了相同的结果
在C#我通常会使用?但不确定ASP.NET VB中的最佳方法
??
与VB.NET中的C#相同的是IF运算符
strLocation = If(IsDBNull(q1("LocationCity")), "", CStr(q1("LocationCity")))
Run Code Online (Sandbox Code Playgroud)
不要使用IIF功能,因为它已被弃用,不支持短路且不是类型安全的.
还看到C#的VB.NET等价吗?运营商?相关信息