To populate dropdown list
To populate dropdown list from data base
TO populatwe dropdown list the following code snippet will be as follows,
firstly i have established the sql connection globally and the same thing follows for SqldataAdapter and DataSet as well and was used when needed,the SqldataAdapter was used as a bridge between DataSet and the server,The DataSet was mainly used to get the element from data base it is as follows(DataSet<-DataAdapter<-DataProvider<-Database).In the pager load event I have established the sqlConnection and the select query was performed in the dataAdapter and was retrived in DataSet through DA.Fillhere i have used an intermediate tabe to store the data temporarily and the dropdown list was populated as shown below
Example:
public partial class Default3 : System.Web.UI.Page
{
string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter DA= new SqlDataAdapter();
DataSet DS = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
DA = new SqlDataAdapter("select * from Category1",con);
DS = new DataSet();
//cmd.ExecuteNonQuery();
DA.Fill(DS,"Table");
DropDownList1.DataSource = DS.Tables[0];
DropDownList1.DataTextField = DS.Tables[0].Columns["CategoryName"].ColumnName.ToString();
DropDownList1.DataValueField = DS.Tables[0].Columns["CategoryID"].ColumnName.ToString();
DropDownList1.DataBind();
}
}