Hi Muhammad,
You can do this by following two ways.
1. By using ListItemCollection
In SqlHelper fetch the data from database and then build ListItemCollection by following way.
In Your SqlHelper class you need to Add the following namespace
System.Web.UI.WebControls.
Public ListItemCollection GetData()
{
ListItemCollection collection = new ListItemCollection();
foreach(var row in db.rows)
{
collection.Add(new ListItem(row[0],row[1]));
}
return collection
}
Now in your presentation layer
ListItemCollection col = GetData();
dropdown.datasource = col;
dropdown.DataBind();
2 By using DataTable.
In Your SqlHelper return the Datatable
public DataTable GetData()
{
DataTable dt = new DataTable();
dt =
return dt;
}
In Your Presentation Layer.
DataTable dt = GetData();
dropDown.DataSource = dt;
dropdown.DataTextField = dt.col[0];
dropdown.DataValueField= dt.col[1];
dropdown.DataBind();
we can also do this by using Dictionary also.
Leave the comment if you want to achieve this by using Dictionary.
Hope this Helps.