Am coding on visual studio 2012 and using Entity Model as my Data layer. However, my drop down control with the Linq statement tend to throw an unhandled exception when the page tries to load (stated title above). Here is my code below;
using (AdventureWorksEntities dw = new AdventureWorksEntities())
{
ddlCon.DataSource = (from em in dw.Employees
select new { em.Title, em.EmployeeID });
ddlCon.DataTextField = "Title";
ddlCon.DataValueField = "EmployeeID";
ddlCon.DataBind();
ddlCon.Items.Insert(0, new ListItem("--Select--", "--Select--"));
}
- I want to know why that error occurred
- What should be the proper way to bind to a control when using LINQ?