How to bind combo Box / or drop down list to the database

How to bind combo Box  or drop down list to the database:

In the given example i am illustrating  binding of the sql Data (only single column named Domain) to the Drop down list. It is quite similar to bind with combobox in windows application



 SqlConnection con = new SqlConnection(constring);
        con.Open();
        SqlCommand cmd = new SqlCommand("", con);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "SELECT DISTINCT Domain from SurveyDomainTable";
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adpt.SelectCommand = cmd;
        adpt.Fill(ds);

        // bind domain with the dropdownlist

        ddlDomain.DataTextField = "Domain";
        ddlDomain.DataValueField = "Domain";
        ddlDomain.DataSource = ds;
        ddlDomain.DataBind();
        con.Close();

Comments