1

I'm trying to bind an ASP.net DropDownList to the results of an entity framework query, while still maintaining multi-tier separation. (i.e. I don't want my UI code to contain query details, nor my Data Layer code to have UI dependencies.) My code-behind in the Page_Load event handler looks like this:

        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's an intermediate business layer as well, but there no processing there as yet -- just a pass-through.):

    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;
        }
    }

When I get to the DocTypeDropDownList.DataBind();, it throws an ObjectDisposedException with the message "DocTypeDropDownList.DataBind();". Can anyone advise me on the best way to tackle this?

Thanks, Andy

AndrewCr
  • 549
  • 9
  • 18
  • Martin: If I add the line you suggest, I get an InvalidOperationException on that new line with the message "The object cannot be detached because it is not attached to the ObjectStateManager." I've tried searching for info on this new message, but found nothing. – AndrewCr Jan 02 '09 at 17:32
  • Ok, seems you have to detach every object in the list/IEnumrable, not the IEnumerable itself. – M4N Jan 02 '09 at 17:35

2 Answers2

2

Don't you have to detach the objects from the context? 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;
M4N
  • 94,805
  • 45
  • 217
  • 260
  • As it turns out, this doesn't fix the problem, and my approach of using .ToArray() doesn't either. If I just detach, i still get the same exception. If I use .ToArray(), I get a different exception later when I try to add the Lookup to another entity as an association. – AndrewCr Jan 02 '09 at 20:39
1

Why don't you 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();
    }
}
erickalves05
  • 273
  • 4
  • 13