Click here to Skip to main content
15,885,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In 3 layers project I get data with store procedure in data access layers and I want convert this to datatable(for bind in gridview).
IQueryable don’t supported from server side paging.


I found this way:
C#
public DataTable GetInfo(int Id)
{
   db = new DataClassesDataContext();
DataTable dt=new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
var query=db.spGetInf(id) .AsQueryable();
    var reader = value.GetEnumerator();

    while (reader.MoveNext())
    {
        var rw =reader.Current;
        dt.Rows.Add(rw.Id, rw.name);
    }

    return dt;
}
Posted
Updated 10-Dec-19 4:02am

1 solution

There's quite a bit of information on how to provide backwards compatibility to ASP.NET gridviews from LINQ out there, specifically have a look at this thread:
http://stackoverflow.com/questions/1595350/why-am-i-not-getting-copytodatatable-in-linq-query[^]
 
Share this answer
 
Comments
m.kolbadi 29-Jul-14 0:48am    
it work but i have one problem,I use Store procedure and your solution dosent work with SP
Nathan Minier 29-Jul-14 9:18am    
I just spent a little time on it when I realized that I'm not nearly as familiar with LINQtoSQL as I should be. It looks like the common solution to to use an ObjectDataSource instead of the LinqDataSource in order to provide SP support. Your solution should be fine, but I would suggest a small change to it due to the Disposable nature of the DataContext.

public DataTable GetInfo(int Id)
{
using(var db = new DataClassesDataContext())
{
DataTable dt=new DataTable();
dt.Columns.Add(“ID”);
dt.Columns.Add(“Name”);
var query=db.spGetInf(id) .AsQueryable();
var reader = value.GetEnumerator();

while (reader.MoveNext())
{
var rw =reader.Current;
dt.Rows.Add(rw.Id, rw.name);
}

return dt;
}
}

This will allow the context to Dispose and will avoid some issues with unmanaged resources down the road.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900