Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i am using Asp.Net C# and i have self refernece table for category #id name Parent_id
for save category i have a text box for name and dropdownlist for parent category and on page_load i fill ddl with recursion and displayCategory() method : is there any better way to fill dropdownlist that show category ?

C#
public DataTable getCats()
        {
            string selectQuery = "SELECT * FROM Category";
            SqlCommand cmd = new SqlCommand(selectQuery);
            return FillDataTable(cmd); 
        }
public void add_Cat(string name,int? parentId)
        {
            SqlConnection con = new SqlConnection(connectionString);

            string insertQuery = "INSERT INTO Category VALUES(@name,@parentId)";

            SqlCommand cmd = new SqlCommand(insertQuery,con);
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@parentId", (parentId.HasValue) ? parentId : (object)DBNull.Value);

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }


        }

protected void btnSaveCat_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtCatName.Text))
                return;
            int? parentId = null;
            if (ddlParentCat.SelectedIndex != 0)
                parentId = int.Parse(ddlParentCat.SelectedValue);

            DBUtil DB = new DBUtil();
            DB.add_Cat(txtCatName.Text,parentId);
            bindToDDL();
        }

        private void bindToDDL()
        {
            ddlParentCat.Items.Clear();
            DBUtil DB = new DBUtil();
            DataTable dt = new DataTable();
            dt=DB.getCats();
            displayCategory(dt,null,0);
            ddlParentCat.Items.Insert(0, new ListItem("Select a category", "0"));
        }

        private void displayCategory(DataTable dt, int? id, int depth)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["Parent_id"].ToString() == id.ToString())
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < depth; i++)
                        sb.Append((char)0XA0);
                    ListItem ls = new ListItem(sb + row["Cat_Name"].ToString(), row["Cat_id"].ToString());
                    if (depth == 0)
                        ls.Attributes.Add("style", "color:red;");
                    ddlParentCat.Items.Add(ls);
                    displayCategory(dt, int.Parse(row["Cat_id"].ToString()), depth + 1);
                }
            }

        }
Posted
Updated 29-Aug-14 0:39am
v4
Comments
Herman<T>.Instance 29-Aug-14 6:37am    
Methods GetCats is missing....
HDM91 29-Aug-14 6:39am    
i added that to code
kbrandwijk 29-Aug-14 11:42am    
Have a look at how to make a dropdownlist contains a treeview in a web?[^]. Does that suit your needs?

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