ASP.Net 错误 - 无法转换类型为“System.String”的对象输入“System.Data.DataTable”
我收到以下错误
无法将“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不是 VB 人员,但我认为您需要将会话变量转换为正确的类型(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):
我认为您需要“投射”Session(“Brief”):
参见示例 此处
I think you need to "cast" Session("Brief") :
see example here
这个怎么样:
使用 TryCast 并检查强制转换是否成功...
下面的版本添加了一些 LINQ 以进行良好的测量:
How about this one:
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: