ASP.Net 错误 - 无法转换类型为“System.String”的对象输入“System.Data.DataTable”

发布于 2024-08-06 20:09:58 字数 512 浏览 9 评论 0原文

我收到以下错误

无法将“System.String”类型的对象转换为“System.Data.DataTable”类型。

这是我使用的代码

Dim str As String = String.Empty

    If (Session("Brief") IsNot Nothing) Then

        Dim dt As DataTable = Session("Brief")
        If (dt.Rows.Count > 0) Then
            For Each dr As DataRow In dt.Rows
                If (str.Length > 0) Then str += ","
                str += dr("talentID").ToString()
            Next
        End If

    End If

    Return str

谢谢

I get the below error

Unable to cast object of type 'System.String' to type 'System.Data.DataTable'.

This is the code I'm using

Dim str As String = String.Empty

    If (Session("Brief") IsNot Nothing) Then

        Dim dt As DataTable = Session("Brief")
        If (dt.Rows.Count > 0) Then
            For Each dr As DataRow In dt.Rows
                If (str.Length > 0) Then str += ","
                str += dr("talentID").ToString()
            Next
        End If

    End If

    Return str

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-08-13 20:09:58

我不是 VB 人员,但我认为您需要将会话变量转换为正确的类型(DataTable):

Dim dt As DataTable = CType(Session("Brief"), DataTable);

I'm not a VB guy, but I would have thought you would need to cast the session variable to the correct type (DataTable):

Dim dt As DataTable = CType(Session("Brief"), DataTable);
家住魔仙堡 2024-08-13 20:09:58

我认为您需要“投射”Session(“Brief”):

Dim dt As DataTable = CType(Session("Brief"), Datatable)

参见示例 此处

I think you need to "cast" Session("Brief") :

Dim dt As DataTable = CType(Session("Brief"), Datatable)

see example here

国际总奸 2024-08-13 20:09:58

这个怎么样:

Dim str As String = ""

If Not Session("Brief") Is Nothing Then
  Dim dt As DataTable = TryCast(Session("Brief"), DataTable)

  If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
    For Each dr As DataRow In dt.Rows
      If (str.Length > 0) Then
        str += ","
      End If

      str += dr("talentID").ToString()
    Next
  End If
End If

Return str

使用 TryCast 并检查强制转换是否成功...

下面的版本添加了一些 LINQ 以进行良好的测量:

Dim str As String = ""

If Not Session("Brief") Is Nothing Then
  Dim dt As DataTable = TryCast(Session("Brief"), DataTable)

  If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
    str = Join((From r In dt Select CStr(r("talentID"))).ToArray, ",")
  End If
End If

Return str

How about this one:

Dim str As String = ""

If Not Session("Brief") Is Nothing Then
  Dim dt As DataTable = TryCast(Session("Brief"), DataTable)

  If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
    For Each dr As DataRow In dt.Rows
      If (str.Length > 0) Then
        str += ","
      End If

      str += dr("talentID").ToString()
    Next
  End If
End If

Return str

Use TryCast and the check of the cast was succesful or not...

And here's version with a bit of LINQ thrown in for good measure:

Dim str As String = ""

If Not Session("Brief") Is Nothing Then
  Dim dt As DataTable = TryCast(Session("Brief"), DataTable)

  If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
    str = Join((From r In dt Select CStr(r("talentID"))).ToArray, ",")
  End If
End If

Return str
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文