ASP.Net Error - Unable to cast object of type 'System.String' to type 'System.Data.DataTable'

xtrabits picture xtrabits · Oct 4, 2009 · Viewed 7.7k times · Source

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

Answer

Mark Bell picture Mark Bell · Oct 4, 2009

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);