Reece Hewitson Reece Hewitson - 2 days ago 6
SQL Question

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>

Answer

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.

Comments