Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:18:06+00:00 2026-05-26T05:18:06+00:00

I am getting an error here in sorting the gridview. My datasource is a

  • 0

I am getting an error here in sorting the gridview. My datasource is a var results, which I am getting thru a Linq query

protected void Page_Load(object sender, EventArgs e)
{
    dt1 = obj1.Table1data().Tables[0];
    dt2 = obj1.Table2data().Tables[0];
    dt3 = obj1.Table3data().Tables[0];

    var results = (
        from table1 in dt1.AsEnumerable()
        join table2 in dt2.AsEnumerable() on (int)table1["id"] equals (int)table2["id"]
        join table3 in dt3.AsEnumerable() on (int)table1["id"] equals (int)table3["id"]

        select new
        {
            id = (int)table1["id"],
            S1= (int)table1["S1"],
            P1= (double)table1["P1"],
            P2= (int)table2["P2"],
            P3= (double)table2["P3"],
            P4 = (int)table3["P4"],
            P5= (double)table3["P5"],

        }).ToList();

    Session["ds"] = results;
    GridView1.DataSource = results;
    GridView1.DataBind();
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataSet dataSet = (DataSet)Session["ds"];
    DataTable dataTable = dataSet.Tables[0];

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridView1.DataSource = dataView;
        GridView1.DataBind();
    }
}

// here in the GridView1_sorting at DataSet dataSet = (DataSet)Session["ds"], I am getting an error

error:

Unable to cast object of type System.Collections.Generic.List`1[<>f__AnonymousType5`8[System.Int32,System.String,System.Int32,System.Double,System.Int32,System.Double,System.Int32,System.Double]]' to type 'System.Data.DataSet'

2) Also another question, What is the datatype of the var results.
Thanks
Sun

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:18:06+00:00Added an answer on May 26, 2026 at 5:18 am

    Session["ds"] holds var results, and results is a List<'A>, where 'A is an anonymous type generated by the compiler. You cannot cast this to a DataSet. If you want to put this into session and retrieve it later, declare a proper class, and then you can easily put the list into and out of Session.

    What I mean is that your query is building an an anonymous type because of the select statement

     select new 
     {
    

    This is normally fine, but you’re trying to use this result beyond the immediate local scope by putting it into session. You need to build a proper class to hold that data. Give it the right properties.

     public class MyData
     {
          // give it the appropriate properties you need
          public int ID { get; set; }
          public int S1 { get; set; }
          public double P1 { get; set; }
          public int P2 { get; set; }
          public double P3 { get; set; }
          public int P4 { get; set; }
          public double P5 { get; set; }
          // by the way... you should really come up with better names 
          // for these properties!
     }
    

    And then make your query

     select new MyData
     {
    

    And when you invoke ToList() and the result, you will have List<MyData>. So when you go to retrieve this from session, this is what you would cast it to.

     var list = (List<MyData>)Session["ds"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am developing parser using bison...in my grammar i am getting this error Here
I'm getting syntax error in firebug here is the code : $('#moderator-attention').live('toogle', function(){ function
Please refer my previous post here . I did changes accordingly but getting error.
I'm getting this error: -[NSCFArray insertObject:atIndex:]: attempt to insert nil Here is the .m
I am getting an error in an ajax call from jQuery. Here is my
I keep getting this error while trying to modify some tables. Here's my code:
I'm getting crazy with a small problem here, I keep getting an error and
Ok... changing the question here... I'm getting an error when I try this: SELECT
Guys i need some help here , im getting this error here at the
I'm getting an error here that says I haven't defined a method, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.