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