Search Asp.Net Here

Bind DropDownList with Xml and DataSet



How to Bind DropDownList with Xml File
How to Bind DropDownList with DataSet
How to Bind DropDownList in Asp.Net

Bind DropDownList with DataSet and Xml File

In a XML file there is a list of employees and now I want to bind the employee IDs and Names to a dropdownlist. Eno (ID) is a DataValueField and ename(name) is DataTextField of a dropdownlist and the names should be in the ascending order.

emp.xml


<employees>
<emp>
<eno>1</eno>
<ename>Rahul Choudhary</ename>
<esal>45000</esal>
</emp>
<emp>
<eno>2</eno>
<ename>Vivek Choudhary</ename>
<esal>3000</esal>
</emp>
<emp>
<eno>3</eno>
<ename>Ram Kumar</ename>
<esal>12000</esal>
</emp>
<emp>
<eno>4</eno>
<ename>Sham Sharma</ename>
<esal>20000</esal>
</emp>
<emp>
<eno>5</eno>
<ename&g;tArun Kumar</ename>
<esal>5000</esal>
</emp>
</employees>

Code sample:

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Bind_DropDownList();
}
}
private void Bind_DropDownList()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));

DataView dv = ds.Tables[0].DefaultView;
//Uncomment below line to filtered Employee according to salary
//dv.RowFilter = "esal>=20000";

dv.Sort = "ename asc";

DropDownList1.DataSource = dv;
DropDownList1.DataValueField = "eno";
DropDownList1.DataTextField = "ename";
DropDownList1.DataBind();
}




Comments :

0 comments to “Bind DropDownList with Xml and DataSet”

Post a Comment