Populate DropDownList based on another using C# and VB.Net in ASP.Net

Last Reply 11 days ago By itsme

Posted 11 days ago

I am trying to add category and sub category from admin side so admin can add category ,subcategory as well as both at same time . Admin can add category ans sub category saperatelly but i want to implement that whena dmin select category from dropdownlist then sub category should be added in that category only .

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="container" style="margin-left: 40%; width: 100%">
        <div class="row">
            <div class="col-sm-4">
                <div class="shadow-lg p-3 mb-5 bg-white rounded panel panel-blue">
                    <div class="panel-heading">
                        <center><b>Add Category</b></center>
                    </div>
                    <div class="panel-body">
                        <b style="font-size:large">Category</b>
                        <asp:DropDownList ID="drpcat" runat="server" class="form-control input-lg" Style="width: 100%; margin-bottom: 10px;" OnSelectedIndexChanged="drpcat_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
                            <asp:ListItem>--Select--</asp:ListItem>
                        </asp:DropDownList>
                        <asp:Button ID="Add_New" runat="server" Text="Add New Category" OnClick="Add_New_Click" CssClass="btn btn-primary" />
                        <asp:Panel ID="pnlcat" runat="server" Visible="false">
                            <b>Add Category:</b>
                            <asp:TextBox ID="txtcat" class="form-control" runat="server" placeholder="Enter Category" />
                        </asp:Panel>
                        <asp:Button ID="addcat" runat="server" Text="Add" OnClick="addcat_Click1" CssClass="btn btn-primary" Visible="false" />
                        <p style="font-size: x-large; font-weight: bolder">Primary Profession</p>
                        <asp:DropDownList ID="drppp" runat="server" class="form-control input-lg" Style="width: 100%; margin-bottom: 10px;" AutoPostBack="true" AppendDataBoundItems="false" >
                            <asp:ListItem>--Select--</asp:ListItem>                           
                        </asp:DropDownList>
                        <asp:Button ID="addsub" runat="server" Text="Add Sub Category" OnClick="addsub_Click" CssClass="btn btn-primary" />
                        <asp:Panel ID="pnlpp" runat="server" Visible="false">
                            <b>Add Sub Category:</b>
                            <asp:TextBox ID="txtpp" class="form-control" runat="server" placeholder="Enter Sub Category" />
                        </asp:Panel>
                        <asp:Button ID="add" runat="server" Text="Add" OnClick="add_Click" CssClass="btn btn-primary" Visible="false" />
                        <center><asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" CssClass="btn btn-primary" /></center>
                    </div>
                </div>
            </div>
        </div>
    </div>
</asp:Content>

 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string query = "select DISTINCT Category from Category_tbl";
                BindDropDownList(drpcat, query, "Category", "Category");
                //DropDownList1.Enabled = false;
                //DropDownList1.Items.Insert(0, new ListItem("0"));

            }
        }
        private void BindDropDownList(DropDownList ddl, string query, string text, string value)
        {
            string conString = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    con.Open();
                    ddl.DataSource = cmd.ExecuteReader();
                    ddl.DataTextField = text;
                    ddl.DataValueField = value;
                    ddl.DataBind();
                    con.Close();
                }
            }
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Category_tbl (Category,Sub_category) VALUES (@Category,@Sub_category)", conn))
                    {

                        cmd.Parameters.AddWithValue("@Category", txtcat.Text.ToString());
                        cmd.Parameters.AddWithValue("@Sub_category", txtpp.Text.ToString());
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Added Successfully.');", true);
                    Response.Redirect("AddCategory.aspx");

                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Error While Adding.');", true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }

        protected void drpcat_SelectedIndexChanged(object sender, EventArgs e)
        {
            string category = drpcat.SelectedItem.Value;
            string query = string.Format("select DISTINCT Sub_category from Category_tbl where Category='{0}'", category);
            BindDropDownList(drppp, query, "Sub_category", "Sub_category");
        }

        protected void addsub_Click(object sender, EventArgs e)
        {
            addsub.Visible = false;
            add.Visible = true;
            pnlpp.Visible = true;
        }

        protected void addcat_Click1(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Category_tbl (Category) VALUES (@Category)", conn))
                    {

                        cmd.Parameters.AddWithValue("@Category", txtcat.Text.ToString());
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Added Successfully.');", true);
                    Response.Redirect("AddCategory.aspx");

                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Error While Adding.');", true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }

        protected void Add_New_Click(object sender, EventArgs e)
        {
            pnlcat.Visible = true;
            Add_New.Visible = false;
            addcat.Visible = true;
        }

        protected void add_Click(object sender, EventArgs e)
        {

        }

 

 

Posted 11 days ago Modified on 11 days ago

Posted 11 days ago

I Found the answer.

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="container" style="margin-left: 40%; width: 100%">
        <div class="row">
            <div class="col-sm-4">
                <div class="shadow-lg p-3 mb-5 bg-white rounded panel panel-blue">
                    <div class="panel-heading">
                        <center><b>Add Category</b></center>
                    </div>
                    <div class="panel-body">
                        <b style="font-size:large">Category</b>
                        <asp:DropDownList ID="drpcat" runat="server" class="form-control input-lg" Style="width: 100%; margin-bottom: 10px;" OnSelectedIndexChanged="drpcat_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
                            <asp:ListItem>--Select--</asp:ListItem>
                        </asp:DropDownList>
                        <asp:Button ID="Add_New" runat="server" Text="Add New Category" OnClick="Add_New_Click" CssClass="btn btn-primary" />
                        <asp:Panel ID="pnlcat" runat="server" Visible="false">
                            <b>Add Category:</b>
                            <asp:TextBox ID="txtcat" class="form-control" runat="server" placeholder="Enter Category" />
                        </asp:Panel>
                        <asp:Button ID="addcat" runat="server" Text="Add" OnClick="addcat_Click1" CssClass="btn btn-primary" Visible="false" />
                        <p style="font-size: x-large; font-weight: bolder">Primary Profession</p>
                        <asp:DropDownList ID="drppp" runat="server" class="form-control input-lg" Style="width: 100%; margin-bottom: 10px;" AutoPostBack="true" AppendDataBoundItems="false" >
                            <asp:ListItem>--Select--</asp:ListItem>                           
                        </asp:DropDownList>
                        <asp:Button ID="addsub" runat="server" Text="Add Sub Category" OnClick="addsub_Click" CssClass="btn btn-primary" />
                        <asp:Panel ID="pnlpp" runat="server" Visible="false">
                            <b>Add Sub Category:</b>
                            <asp:TextBox ID="txtpp" class="form-control" runat="server" placeholder="Enter Sub Category" />
                        </asp:Panel>
                        <asp:Button ID="add" runat="server" Text="Add" OnClick="add_Click" CssClass="btn btn-primary" Visible="false" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</asp:Content>

 

    public partial class AddCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string query = "select DISTINCT Category from Category_tbl";
                BindDropDownList(drpcat, query, "Category", "Category");
                //DropDownList1.Enabled = false;
                //DropDownList1.Items.Insert(0, new ListItem("0"));
            }
        }
        private void BindDropDownList(DropDownList ddl, string query, string text, string value)
        {
            string conString = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    con.Open();
                    ddl.DataSource = cmd.ExecuteReader();
                    ddl.DataTextField = text;
                    ddl.DataValueField = value;
                    ddl.DataBind();
                    con.Close();
                }
            }
        }
        protected void drpcat_SelectedIndexChanged(object sender, EventArgs e)
        {
            string category = drpcat.SelectedItem.Value;
            string query = string.Format("select DISTINCT Sub_category from Category_tbl where Category='{0}' and Sub_category!=' '", category);
            BindDropDownList(drppp, query, "Sub_category", "Sub_category");
        }

        protected void addsub_Click(object sender, EventArgs e)
        {
            addsub.Visible = false;
            add.Visible = true;
            pnlpp.Visible = true;
        }

        protected void addcat_Click1(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Category_tbl (Category) VALUES (@Category)", conn))
                    {
                        cmd.Parameters.AddWithValue("@Category", txtcat.Text.ToString());
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Added Successfully.');", true);
                    Response.Redirect("AddCategory.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Error While Adding.');", true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }

        protected void Add_New_Click(object sender, EventArgs e)
        {
            pnlcat.Visible = true;
            Add_New.Visible = false;
            addcat.Visible = true;
        }

        protected void add_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                string drpvalue = drpcat.SelectedValue.ToString();
                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Category_tbl (Category,Sub_category) VALUES (@Category,@Sub_category)", conn))
                    {

                        cmd.Parameters.AddWithValue("@Category", drpvalue);
                        cmd.Parameters.AddWithValue("@Sub_category", txtpp.Text.ToString());
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Added Successfully.');", true);
                    Response.Redirect("AddCategory.aspx");

                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Error While Adding.');", true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
    }