Csharp
Csharp

Reputation: 2982

Unable to cast object of type 'System.Collections.Generic.List

I am getting this error

Unable to cast object of type 'System.Collections.Generic.List`1[TransTripLeg]' to type 'System.Data.DataTable'.

and I can't seem to figure out how to resolve it. The error occurs at DataTable dtTable = in the code below.

if (!string.IsNullOrEmpty(Request.QueryString["EditMode"]))
{
    DataTable dtUpdate = (DataTable)Session["TripRecords"];
    DataRow[] customerRow = dtUpdate.Select("LegID = '" + sLegID.ToString() + "'");

I think I am casting it to type DataTable, but then I still get the error.

Upvotes: 2

Views: 19441

Answers (2)

Asif Mahamud
Asif Mahamud

Reputation: 583

Remove this symbol just works for me(') this works fine for me

DataRow[] dr = allleaveDt.Select("EmployeeId=" + empId);

Upvotes: 1

Win
Win

Reputation: 62301

You need to cast Session["TripRecords"] to List<TransTripLeg> instead of DataTable

var collection = (List<TransTripLeg>)Session["TripRecords"];
var legs = collection.Where(c => c.LegID == sLegID);

Upvotes: 4

Related Questions