Databinding ASP.net DropDownList with Entity Framework
I am trying to bind ASP.NET DropDownList to the results of an entity structure query while maintaining tiered separation. (i.e. I don't want my UI code to contain the request details, nor do my Data Layer code to have UI dependencies.) My code 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();
Whereas my data code looks like this (there is also an intermediate business layer, but there is no processing there yet - just a walkthrough.):
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 hit DocTypeDropDownList.DataBind (); it throws an ObjectDisposedException with the message "DocTypeDropDownList.DataBind ();". Can anyone advise me on the best way to handle this?
Thanks, Andy
+1
source to share