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
AndrewCr
source
to share
2 answers
Don't need to separate objects from context? For example:
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
M4N
source
to share
Why don't you just use 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();
}
}
+1
erickalves05
source
to share