Populate Dropdownlist from Database - ASP.NET


 Dropdownlist in ASP.NET can be binded with values from database table.  The below  example C# code snippet shows how to populate asp.net drop down list values from database.

private void FillDropDownList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);

        SqlCommand cmd = new SqlCommand("Select * from students", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);
       
        DropDownList1.DataTextField = ds.Tables[0].Columns["FullName"].ToString();
        DropDownList1.DataValueField = ds.Tables[0].Columns["id"].ToString();

        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataBind();
    }