简体   繁体   中英

How to create dynamic dropdownlist with datasource from database

I trying to create dynamic dropdownlist with datasource from my access database. I want to add the dropdownlist into a table. Tried to do like this but it didn't work. How can I do it, and get the value if a item selected in dropdownlist? Thank you.

DropDownList mylist = new DropDownList();
            mylist.DataSource = DBConnectivity.getMovieSchedule(c.Movie_ID);
            mylist.DataTextField = "date";
            mylist.DataValueField = "id";
            mylist.DataBind();
            c6.Controls.Add(mylist);

This should do it for you.

using (SqlConnection sqlConnection = new SqlConnection("connstring"))
{
    SqlCommand sqlCmd = new SqlCommand("SELECT * FROM YourTable", sqlConnection);
    sqlConnection.Open();
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();

    while (sqlReader.Read())
    {
        ComboBox1.Items.Add(sqlReader["name"].ToString());
    }

    sqlReader.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM