我收到以下错误
无法将'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
由于
答案 0 :(得分:2)
我不是VB人,但我认为你需要将会话变量强制转换为正确的类型(DataTable):
Dim dt As DataTable = CType(Session("Brief"), DataTable);
答案 1 :(得分:1)
答案 2 :(得分:1)
这个怎么样:
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