4

I need a very simple example of code to populate a DropDownList using Entity Framework 4.

At the moment I use this code:

        using (TestHierarchyEntities context = new TestHierarchyEntities())
        {
            uxSelectNodeDestinationDisplayer.DataSource = context.CmsCategories.ToList();
            uxSelectNodeDestinationDisplayer.DataBind();
        }

But it does not work properly... Any idea? Thanks

4
  • Have you set the DataTextField and DataValueField on the drop down list? When you say it hasn't worked properly, what do you mean; was there an exception, unexpected values in the drop down, nothing in the drop down?? Commented Jan 17, 2011 at 11:07
  • Hi dave, thansk for your message. Could you post an example of code using DataTextField and DataValueField? Thanks Commented Jan 17, 2011 at 11:56
  • There's a full description of the control available at msdn.microsoft.com/en-gb/library/… but to keep it short and sweet, you should have something like: uxSelectNodeDestinationDisplayer.DataTextField = "Description"; uxSelectNodeDestinationDisplayer.DataValueField = "CategoryID"; obviously you'll need to subsitute in the correct field names from the entity you're binding to the control. Commented Jan 17, 2011 at 12:02
  • Hi, I tried your links but I am still not able to do it pro grammatically. I found a quick solution... at the moment I use Entity Data Source so I am able to bind the Drop Down Menu using EF. Thansk for now! Commented Jan 17, 2011 at 15:13

3 Answers 3

8

Something like this should work :

using (TestHierarchyEntities context = new TestHierarchyEntities())
        {
                var category = (from c in context.context
                                select new { c.ID, c.Desc }).ToList();

                DropDownList1.DataValueField = "MID";
                DropDownList1.DataTextField = "MDesc";
                DropDownList1.DataSource = category;
                DropDownList1.DataBind();                
         }
Sign up to request clarification or add additional context in comments.

Comments

1

This works perfectly:

private COFFEESHOPEntities1 CoffeeContext = new COFFEESHOPEntities1();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //getData();
        cbxCategory.DataSource = CoffeeContext.tblProductTypes.ToList();
        cbxCategory.DataTextField = "Description";
        cbxCategory.DataValueField = "ProductType";
        cbxCategory.DataBind();
    }
}

Comments

0
using (dbEntities db = new dbEntities())
{
    ddlNewEmployee.DataSource = (from emp in db.CC_EMPLOYEE
                                    select new { emp.EmployeeID, emp.EmployeeCode }).ToList();

    ddlNewEmployee.DataTextField = "EmployeeCode";
    ddlNewEmployee.DataValueField = "EmployeeID";
    ddlNewEmployee.DataBind();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.