Here is an example that gets the category names and status and populates the checkbox - you will need to adapt it to your own needs.
I've assumed that
category_status
equates to
true
or
false
- for example it might be a
bit
column where 0 = false and 1 = true.
I haven't faffed around with category and sub-category because you haven't really described the relationship and because the example lists
all from the table, regardless of whether they have a "parent" or not.
One final thing to note - it is bad practice to use
Select * from (category_data]
Better is use the explicit column names that you need:
SELECT category_name, category_status FROM category_data
Here is a working example that populates a ComboBoxList and automatically sets the selected value based on category_status.
private void BindCheckBoxList()
{
var constr = ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString;
using (var con = new SqlConnection(constr))
{
using (var cmd = new SqlCommand("SELECT category_name, category_status FROM category_data"))
{
using (var sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (var ds = new DataSet())
{
sda.Fill(ds);
if (ds.Tables[0].Rows.Count <= 0) return;
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "category_name";
CheckBoxList1.DataValueField = "category_name";
CheckBoxList1.DataBind();
var i = 0;
foreach (DataRow r in ds.Tables[0].Rows)
{
Debug.Print("{0} {1}", r.ItemArray[0],r.ItemArray[1]);
if (r.ItemArray[1].ToString().ToLower() == "true")
CheckBoxList1.Items[i].Selected = true;
i++;
}
}
}
}
}