Reece Hewitson
Reece Hewitson

Reputation: 91

Binding a datatable to a checkbox list

I have a database with criteria in it, I use SQL to extract all criteria from the database into a data table but when I bind the data it doesn't display in my checkbox? What am I doing wrong?

private void Criteria_Load()
{
   string query = "Select CRITERIA From EF_CONTACT_FIELDS";
   cbCriteria.DataTextField = "CRITERIA";
   cbCriteria.DataSource = GetData(query);
   cbCriteria.DataBind();
}

<asp:CheckBoxList ID="cbCriteria" runat="server"></asp:CheckBoxList>

Upvotes: 1

Views: 4607

Answers (1)

Sankar
Sankar

Reputation: 7117

You have to mention DataTextField to show the selected data.

If you have ID field for CRITERIA and want to get the selected value, you can use, cbCriteria.DataValueField = "CRITERIA_ID"; (if any)

private void Criteria_Load()
    {
        string query = "Select CRITERIA From EF_CONTACT_FIELDS";
        cbCriteria.DataTextField = "CRITERIA";
        cbCriteria.DataSource = GetData(query);
        cbCriteria.DataBind();
    }

Note : DataTextField should be assigned before DataBind.

Upvotes: 1

Related Questions