Problem with query.CopyToDataTable() [modified]
-
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
-
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
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
-
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
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();
-
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();
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
-
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
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.
-
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.
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