Ye Kyaw Kyaw Htoo
Ye Kyaw Kyaw Htoo

Reputation: 712

How do I populate my CheckBoxList with values from my database?

How do I set the CheckBoxList values with my values stored in my database? I have tried using a datareader but it doesn't work and I don't know why.

My Code:

cmd = new SqlCommand("SELECT [SeatID], [Flag] FROM [SeatingPlan] WHERE ([SectionID] = 1 )", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
    for (int i = 0; i < 15; i++)
    {
        if (dr.GetString(1).Equals("true"))
        {
            chkBoxDaysList.Items[i].Selected = true;
            chkBoxDaysList.Items[i].Enabled = false;
        }
    }
}

Upvotes: 1

Views: 12475

Answers (1)

Aditya Pratap Singh
Aditya Pratap Singh

Reputation: 108

you should use data adapter instead of using data reader.

and bind it like this.

chkBoxDaysList.DataSource = tempds.Tables(0)
chkBoxDaysList.DataTextField = "SeatID"
chkBoxDaysList.DataValueField = "Flag"
chkBoxDaysList.DataBind()

Upvotes: 3

Related Questions