Binding a GridView with an IQueryable <T>

This question is purely academic, because I would never dream of doing this in real code.

Using LINQ to SQL, I want to bind IQueryable<T>to a GridView. I tried to do this with the following code, but I get an exception:

Unable to access the remote object. Object Name: "Access to the DataContext after Dispose.".

Here is my code that gets IQueryable<tabGenre>using LINQ to SQL:

public static IQueryable<lu_Genre> GetGenres2() {
    using (BooksContextDataContext ctx = new BooksContextDataContext()) {
        IQueryable<tabGenre> iq = from g in ctx.tabGenre
                                  select g;
        return iq;
    }
}

And here is my code that binds the GridView to the returned one IQueryable<T>.

private void GetGenres() {
    gvGenre.DataSource = Genre.GetGenres2();
    gvGenre.DataBind();
}

? .ToList<tabGenre>(), , , , IQueryable ? , , - , IQueryable.

EDIT: , .

+5
3

IQueryable , . .ToList(), IQueryable(), . IQueryable(), , DataBind().

gvGenre.DataSource() = Genre.GetGenres2(), DataContext, IQueryable, DataBind().

, .ToList(), , .

IQueryable . , , , .

+16

ctx .

iq ctx. ctx , iq - .

iq (, iq = iq.ToList()) . , , . ctx - , . , , . , , ? , , .

+2
    public IQueryable MembershipGetAll()
    {


            var obj = (from mem in db.GetTable<MEMBERSHIP>()
                           select new
                           {
                               NAME = mem.MEMBERSHIP.NAME,
                               TYPE = mem.MEMBERSHIP.TYPE,
                               TAX_ID = mem.TAX_RATE.TAX_ID,
                               DISCOUNT = mem.DISCOUNT,
                               DISCOUNT_PERCENT = mem.DISCOUNT_PERCENT,

                           }
                          ).AsQueryable();
            return obj;

    }


private void LoadMembership()
{
    IQueryable mem = null;
    mem = eb.MembershipGetAll();
    grdMembership.DataSource = mem;
    grdMembershipRates.DataBind();
}

0

All Articles