ASP.NET DropDownList Databinding with Entity Framework

3-tier asp.net data-binding entity-framework

Question

I'm attempting to preserve multi-tier separation while binding the results of an entity framework query to an ASP.net DropDownList. (For example, I don't want the Data Layer code to rely on the UI or have query information in it.) This is how my code-behind for the Page Load event handler looks:

        IEnumerable<Lookup> TypesLookup = Business.DocumentBO.GetDocumentTypes(_LookupTypeID);
        DocTypeDropDownList.DataSource = TypesLookup;
        DocTypeDropDownList.DataTextField = "Description";
        DocTypeDropDownList.DataValueField = "LookupID";
        DocTypeDropDownList.DataBind();

While my data code looks like this (there is also an intermediary business layer, but at this time there is just pass-through processing there):

    public static IEnumerable<Lookup> GetLookups(int LookupTypeID)
    {
        using (VLFDocumentEntities context = new VLFDocumentEntities())
        {
            IEnumerable<Lookup> l = (from c in context.Lookup
                        where c.LookupTypeID == LookupTypeID
                        select c);

            return l;
        }
    }

The ObjectDisposedException with the message "DocTypeDropDownList.DataBind();" is thrown when I reach the DocTypeDropDownList.DataBind();. Can someone please give me some advice on how to approach this?

Regards, Andy

1
1
1/2/2009 5:09:29 PM

Accepted Answer

Do you not need to remove the things from their surroundings? E.g:

IEnumerable<Lookup> l = (from c in context.Lookup
                        where c.LookupTypeID == LookupTypeID
                        select c);
foreach (Lookup lookup in l)
  context.Detach(lookup);
return l;
2
1/2/2009 5:37:33 PM

Popular Answer

Why not just use a List?

public static List<Lookup> GetLookups(int LookupTypeID)
{
    using (VLFDocumentEntities context = new VLFDocumentEntities())
    {
        return (from c in context.Lookup
                    where c.LookupTypeID == LookupTypeID
                    select c).ToList();
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow