There are different methods to load a data in the dropdown control. Some of the common methods that are used are xml, hashtable, arraylist and database. The each method is discussed briefly as follows.
Load Data from XML File
Create a XML file Data.xml as shown below.
1 TestCompany1 Tamilnadu 2 TestCompany2 Andra 3 TestCompany3 Kerala 4 TestCompany4 dubai
Open the asp.net application and drop the dropdown list in the default.aspx file and change the Id as “DDLCompany”
<asp:dropdownlist id="DDLCompany" runat="server" width="212px">
</asp:dropdownlist>
Write the following code in the code’s section. In this program the company names is loaded into the dropdown list with a unique value as an ID.
protected void Page_Load(object sender, EventArgs e)
{
LoadXMLData();
}
protected void LoadXMLData()
{
DataSet ds = new DataSet();
ds.ReadXml("c:/Data/Data.xml");
//get the dataview of table "Country", which is default table
name
DataView dv = ds.Tables["Details"].DefaultView;
//now define datatext field and datavalue field of dropdownlist
DDLCompany.DataTextField = "Company";
DDLCompany.DataValueField = "ID";
DDLCompany.DataSource = dv;
DDLCompany.DataBind();
}
Output

Load Data from Hashtable
The Hashtable contains the key value pairs. The key will be used as indexes. Add() method will be used to add the items.
The procedure LoadHashtableData() is used to load the hashtable Data in to the Dropdown list
protected void Page_Load(object sender, EventArgs e)
{
LoadXMLData();
}
private void LoadHashtableData()
{
Hashtable hTable = new Hashtable();
hTable.Add("1", "Item 1");
hTable.Add("2", "Item 2");
hTable.Add("3", "Item 3");
hTable.Add("4", "Item 4");
dropHashtable.DataSource = hTable.Values;
dropHashtable.DataBind();
}
Output

Load Data From ArrayList
The arraylist can be used to add,delete and sorting collections. An arraylist will act as a datasource for the dropdownlist. In the following example LoadArrayList() will load the arraylist data into the dropdown list.
protected void Page_Load(object sender, EventArgs e)
{
LoadArrayList();
}
protected void LoadArrayList()
{
ArrayList MyArray = new ArrayList();
MyArray.Add("Asp.net");
MyArray.Add("Vb.net");
MyArray.Add("Java");
MyArray.Add("HTML");
MyArray.Add("Java Script");
DDLName.DataSource = MyArray;
DDLName.DataBind();
}
Output

Load Data from Database
The data can be retrieved from the database using the following method.
Create a Sqlconnection using the server name, database with username and password. Create the SQLcommand Object to build up the query to get the data from the table. The data table is created by the DataAdapter object. Select Id and Name from the emp table and use DataTextField and DataValueField properties to display the Name of the employee and Id in the dropdownlist.
protected void Page_Load(object sender, EventArgs e)
{
LoadDbData();
}
protected void LoadDbData()
{
SqlConnection con = new SqlConnection("data source=USER11\\USER112008; Initial Catalog=Testing;User ID=sa;Password=acsadmin;");
con.Open();
SqlCommand cmd = new SqlCommand("select Id,Name from emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DDLName.DataSource = dt;
DDLName.DataTextField = "Name";
DDLName.DataValueField = "Id";
DDLName.DataBind();
cmd.Dispose();
con.Close();
}
Output
