Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello,

how to fill checkbox list with category and sub-category from DB

this is my sql table 
<pre lang="SQL">SELECT
dbo.category_data.category_id,
dbo.category_data.category_name,
dbo.category_data.category_parent,
dbo.category_data.category_description,
dbo.category_data.category_image,
dbo.category_data.category_status

FROM [dbo].[category_data]


and this is my c# code i need help to complete it

C#
private void bind_cat_menu()
   {
       SqlConnection con= new SqlConnection();
       con.ConnectionString = ConfigurationManager.ConnectionStrings["QMALL"].ToString();
       con.Open();
       DataSet ds = new DataSet();
       DataTable dt = new DataTable();
       string sql = "Select * from (category_data]";
       SqlDataAdapter da = new SqlDataAdapter(sql, con);
       da.Fill(ds);
       dt = ds.Tables[0];
       DataRow[] drowparent = dt.Select("category_parent=" + 0);
       foreach (DataRow dr in drowparent)
       {

       }
       foreach (DataRow dr in dt.Select("category_parent>" + 0))
       {

       }


What I have tried:

private void bind_cat_menu()
    {
        SqlConnection con= new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["QMALL"].ToString();
        con.Open();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        string sql = "Select * from (category_data]";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        da.Fill(ds);
        dt = ds.Tables[0];
        DataRow[] drowparent = dt.Select("category_parent=" + 0);
        foreach (DataRow dr in drowparent)
        {
            
        }
        foreach (DataRow dr in dt.Select("category_parent>" + 0))
        {
            
        }
Posted
Updated 2-Mar-17 1:51am

1 solution

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
SQL
Select * from (category_data]
Better is use the explicit column names that you need:
SQL
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.
C#
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;   //nothing to do if no data on the table
                    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++;
                    }
                }
            }
        }
    }
 
Share this answer
 
Comments
Eng Abdull A-ziz 4-Mar-17 5:30am    
its working but what i need is to show the sub-cat to be as a node from the parent
CHill60 5-Mar-17 19:14pm    
Then you may be using the wrong control. Research Tree controls
Eng Abdull A-ziz 7-Mar-17 6:16am    
does the Tree allow multi selection
CHill60 7-Mar-17 18:43pm    
That depends on the tree control you use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900