Sunday 29 November 2015

How to Bind Dropdownlist in Asp.net using entity framework

Question :

How can we bind dropdown list in Asp.net from a table (tbl_department) using entity framework where tbl_department consist of departmentId and departName ?


Solution

Designer.aspx

<asp:DropDownList ID="ddlDepartment" runat="server" AutoPostBack="true">

</asp:DropDownList>


code.cs


db_EmployeeManagEntities context = new db_EmployeeManagEntities();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDepartment();
}
}

private void bindDesignation()
{
var designation = from d in context.tbl_designation select new { d.designationId, d.designationName };
ddlDesignation.DataSource = designation;
ddlDesignation.DataTextField = "designationName";
ddlDesignation.DataValueField = "designationId";
ddlDesignation.DataBind();
ddlDesignation.Items.Insert(0, " --Select Designation--");
}




No comments:

Post a Comment