Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Problem with query.CopyToDataTable() [modified]

Problem with query.CopyToDataTable() [modified]

Scheduled Pinned Locked Moved LINQ
databasehelp
6 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I am using datacontext object and i have a .dbml file. By using datacontext object i am retrieving the database table. I want copy the var query result into DataTable. I tried in the following way. It compiles successfully. But when i ran this i got an InvalidCast Exception. IEnumerable<DataRow> query = (IEnumerable<DataRow>)(from temp in DataContext.APP_USERs.AsEnumerable() select temp); // Here i am getting runtime error. DataTable dt = (DataTable)query.CopyToDataTable(); GridView2.DataSource = query; GridView2.DataBind(); any suggestions. thanks in advance.

    modified on Wednesday, August 27, 2008 6:07 AM

    H 1 Reply Last reply
    0
    • K K V Sekhar

      Hi all, I am using datacontext object and i have a .dbml file. By using datacontext object i am retrieving the database table. I want copy the var query result into DataTable. I tried in the following way. It compiles successfully. But when i ran this i got an InvalidCast Exception. IEnumerable<DataRow> query = (IEnumerable<DataRow>)(from temp in DataContext.APP_USERs.AsEnumerable() select temp); // Here i am getting runtime error. DataTable dt = (DataTable)query.CopyToDataTable(); GridView2.DataSource = query; GridView2.DataBind(); any suggestions. thanks in advance.

      modified on Wednesday, August 27, 2008 6:07 AM

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      Are DataRow and DataTable from a dataset? If so with LINQ to SQL you don't need them. I also note that you set the DataSource of the gridview to the QUERY and not the dt (datatable) ? Unless of course DataRow is just some class type that represents your object

      'Howard

      K 1 Reply Last reply
      0
      • H Howard Richards

        Are DataRow and DataTable from a dataset? If so with LINQ to SQL you don't need them. I also note that you set the DataSource of the gridview to the QUERY and not the dt (datatable) ? Unless of course DataRow is just some class type that represents your object

        'Howard

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        DataRow and DataTable not from DataSet, I used .dbml file(it contains all the tables) IEnumerable<DataRow> query = (IEnumerable<DataRow>(from temp in DataContext.APP_USERs.AsEnumerable() select temp); DataTable dt = (DataTable)query.CopyToDataTable(); //Here Invalid Cast exception GridView2.DataSource = dt;//Here i typed wrong,instead of dt i wrote query GridView2.DataBind();

        H 1 Reply Last reply
        0
        • K K V Sekhar

          DataRow and DataTable not from DataSet, I used .dbml file(it contains all the tables) IEnumerable<DataRow> query = (IEnumerable<DataRow>(from temp in DataContext.APP_USERs.AsEnumerable() select temp); DataTable dt = (DataTable)query.CopyToDataTable(); //Here Invalid Cast exception GridView2.DataSource = dt;//Here i typed wrong,instead of dt i wrote query GridView2.DataBind();

          H Offline
          H Offline
          Howard Richards
          wrote on last edited by
          #4

          Why not use GridView2.DataSource = query; You don't need to convert it to a datatable to bind it.

          var query = from temp in DataContext.APP_USERS select temp;
          GridView2.DataSource = query;
          GridView2.DataBind();

          'Howard

          K 1 Reply Last reply
          0
          • H Howard Richards

            Why not use GridView2.DataSource = query; You don't need to convert it to a datatable to bind it.

            var query = from temp in DataContext.APP_USERS select temp;
            GridView2.DataSource = query;
            GridView2.DataBind();

            'Howard

            K Offline
            K Offline
            K V Sekhar
            wrote on last edited by
            #5

            Yes i can use that, but the problem is sorting & paging of gridview. Paging of grid view is OK. but when i did sorting on specific column(descending), then i do paging, the gridview is paging in ascending order(which initially i used to bind gridview. FillGrid(). In this method i wrote the code as you told. If we can make this query result 'var query' into DataTable, then we can make sorting and paging very easily by using DataView. Thats one of the reason i want to convert var query into datatable. The 2nd reason is, if we want to bind different datasources by querying a table depending upon user selected options of diffrent dropdown values(for ex. country, state, district ect.) for this i have to use the same 'var query' in diffrent events and methods for diffrent purposes. So i thought that if we could able to convert that into datatable its make easy for all those purposes.

            H 1 Reply Last reply
            0
            • K K V Sekhar

              Yes i can use that, but the problem is sorting & paging of gridview. Paging of grid view is OK. but when i did sorting on specific column(descending), then i do paging, the gridview is paging in ascending order(which initially i used to bind gridview. FillGrid(). In this method i wrote the code as you told. If we can make this query result 'var query' into DataTable, then we can make sorting and paging very easily by using DataView. Thats one of the reason i want to convert var query into datatable. The 2nd reason is, if we want to bind different datasources by querying a table depending upon user selected options of diffrent dropdown values(for ex. country, state, district ect.) for this i have to use the same 'var query' in diffrent events and methods for diffrent purposes. So i thought that if we could able to convert that into datatable its make easy for all those purposes.

              H Offline
              H Offline
              Howard Richards
              wrote on last edited by
              #6

              An IQueryable is the datasouce + query. If you apply a LINQ paging function, e.g. Skip(), Take() it uses server-side paging. However, if you convert it to a DataTable it loads ALL the data, regardless of any later paging filters. So IQueryable will load 10 records for a 10-item page, and the DataTable will load the whole query. http://blogs.msdn.com/charlie/archive/2007/12/09/deferred-execution.aspx[^]

              'Howard

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups