加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET VB – 从类型’DBNull’到类型’String’的转换无效

发布时间:2020-12-16 03:58:44 所属栏目:asp.Net 来源:网络整理
导读:我有以下ASP.NET(VB)代码: strLocation = CStr(q1("LocationName")) + " " + CStr(q1("LocationAddress")) + " " + CStr(q1("LocationCity")) 由于LocationCity为null: 我从类型’DBNull’到类型’String’的转换无效. 有没有办法来解决这个问题. 如果它只
我有以下ASP.NET(VB)代码:

strLocation = CStr(q1("LocationName")) + " " + CStr(q1("LocationAddress")) + " " + CStr(q1("LocationCity"))

由于LocationCity为null:

我从类型’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中的最佳方法

解决方法

相当于C#??在VB.NET中是 IF Operator

strLocation = If(IsDBNull(q1("LocationCity")),"",CStr(q1("LocationCity")))

不要使用IIF Function,因为它已被弃用,不支持短路且不是类型安全的.

有关相关信息,请参阅Is there a VB.NET equivalent for C#’s ?? operator?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读