ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Populate DropDownList based on another using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below links -</p> <h1 class="header"><a title="Cascading DropDownList for Country/State/City in ASP.Net'a" href="https://www.aspsnippets.com/Articles/Cascading-DropDownList-for-CountryStateCity-in-ASPNet.aspx">Cascading DropDownList for Country/State/City in ASP.Net</a></h1>https://www.aspforums.net:443/Threads/150040/Populate-DropDownList-based-on-another-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/150040/Populate-DropDownList-based-on-another-using-C-and-VBNet-in-ASPNet/Wed, 04 Sep 2019 00:25:53 GMTPopulate DropDownList based on another using C# and VB.Net in ASP.Net<p>I Found the answer.</p> <pre class="brush: html">&lt;asp:Content ID=&#34;Content2&#34; ContentPlaceHolderID=&#34;ContentPlaceHolder1&#34; runat=&#34;server&#34;&gt; &lt;div class=&#34;container&#34; style=&#34;margin-left: 40%; width: 100%&#34;&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-sm-4&#34;&gt; &lt;div class=&#34;shadow-lg p-3 mb-5 bg-white rounded panel panel-blue&#34;&gt; &lt;div class=&#34;panel-heading&#34;&gt; &lt;center&gt;&lt;b&gt;Add Category&lt;/b&gt;&lt;/center&gt; &lt;/div&gt; &lt;div class=&#34;panel-body&#34;&gt; &lt;b style=&#34;font-size:large&#34;&gt;Category&lt;/b&gt; &lt;asp:DropDownList ID=&#34;drpcat&#34; runat=&#34;server&#34; class=&#34;form-control input-lg&#34; Style=&#34;width: 100%; margin-bottom: 10px;&#34; OnSelectedIndexChanged=&#34;drpcat_SelectedIndexChanged&#34; AutoPostBack=&#34;true&#34; AppendDataBoundItems=&#34;true&#34;&gt; &lt;asp:ListItem&gt;--Select--&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;Add_New&#34; runat=&#34;server&#34; Text=&#34;Add New Category&#34; OnClick=&#34;Add_New_Click&#34; CssClass=&#34;btn btn-primary&#34; /&gt; &lt;asp:Panel ID=&#34;pnlcat&#34; runat=&#34;server&#34; Visible=&#34;false&#34;&gt; &lt;b&gt;Add Category:&lt;/b&gt; &lt;asp:TextBox ID=&#34;txtcat&#34; class=&#34;form-control&#34; runat=&#34;server&#34; placeholder=&#34;Enter Category&#34; /&gt; &lt;/asp:Panel&gt; &lt;asp:Button ID=&#34;addcat&#34; runat=&#34;server&#34; Text=&#34;Add&#34; OnClick=&#34;addcat_Click1&#34; CssClass=&#34;btn btn-primary&#34; Visible=&#34;false&#34; /&gt; &lt;p style=&#34;font-size: x-large; font-weight: bolder&#34;&gt;Primary Profession&lt;/p&gt; &lt;asp:DropDownList ID=&#34;drppp&#34; runat=&#34;server&#34; class=&#34;form-control input-lg&#34; Style=&#34;width: 100%; margin-bottom: 10px;&#34; AutoPostBack=&#34;true&#34; AppendDataBoundItems=&#34;false&#34; &gt; &lt;asp:ListItem&gt;--Select--&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;addsub&#34; runat=&#34;server&#34; Text=&#34;Add Sub Category&#34; OnClick=&#34;addsub_Click&#34; CssClass=&#34;btn btn-primary&#34; /&gt; &lt;asp:Panel ID=&#34;pnlpp&#34; runat=&#34;server&#34; Visible=&#34;false&#34;&gt; &lt;b&gt;Add Sub Category:&lt;/b&gt; &lt;asp:TextBox ID=&#34;txtpp&#34; class=&#34;form-control&#34; runat=&#34;server&#34; placeholder=&#34;Enter Sub Category&#34; /&gt; &lt;/asp:Panel&gt; &lt;asp:Button ID=&#34;add&#34; runat=&#34;server&#34; Text=&#34;Add&#34; OnClick=&#34;add_Click&#34; CssClass=&#34;btn btn-primary&#34; Visible=&#34;false&#34; /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/asp:Content&gt; </pre> <p>&nbsp;</p> <pre class="brush: csharp"> public partial class AddCategory : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string query = &#34;select DISTINCT Category from Category_tbl&#34;; BindDropDownList(drpcat, query, &#34;Category&#34;, &#34;Category&#34;); //DropDownList1.Enabled = false; //DropDownList1.Items.Insert(0, new ListItem(&#34;0&#34;)); } } private void BindDropDownList(DropDownList ddl, string query, string text, string value) { string conString = ConfigurationManager.ConnectionStrings[&#34;Q_SS_regnConnectionString1&#34;].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(&#34;select DISTINCT Sub_category from Category_tbl where Category=&#39;{0}&#39; and Sub_category!=&#39; &#39;&#34;, category); BindDropDownList(drppp, query, &#34;Sub_category&#34;, &#34;Sub_category&#34;); } 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[&#34;Q_SS_regnConnectionString1&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;INSERT INTO Category_tbl (Category) VALUES (@Category)&#34;, conn)) { cmd.Parameters.AddWithValue(&#34;@Category&#34;, txtcat.Text.ToString()); conn.Open(); i = cmd.ExecuteNonQuery(); conn.Close(); } } if (i &gt; 0) { ClientScript.RegisterStartupScript(GetType(), &#34;alert&#34;, &#34;alert(&#39;Added Successfully.&#39;);&#34;, true); Response.Redirect(&#34;AddCategory.aspx&#34;); } else { ClientScript.RegisterStartupScript(GetType(), &#34;alert&#34;, &#34;alert(&#39;Error While Adding.&#39;);&#34;, 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[&#34;Q_SS_regnConnectionString1&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;INSERT INTO Category_tbl (Category,Sub_category) VALUES (@Category,@Sub_category)&#34;, conn)) { cmd.Parameters.AddWithValue(&#34;@Category&#34;, drpvalue); cmd.Parameters.AddWithValue(&#34;@Sub_category&#34;, txtpp.Text.ToString()); conn.Open(); i = cmd.ExecuteNonQuery(); conn.Close(); } } if (i &gt; 0) { ClientScript.RegisterStartupScript(GetType(), &#34;alert&#34;, &#34;alert(&#39;Added Successfully.&#39;);&#34;, true); Response.Redirect(&#34;AddCategory.aspx&#34;); } else { ClientScript.RegisterStartupScript(GetType(), &#34;alert&#34;, &#34;alert(&#39;Error While Adding.&#39;);&#34;, true); } } catch (Exception ex) { Response.Write(ex); } } }</pre> <p>&nbsp;</p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/150040/Populate-DropDownList-based-on-another-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/150040/Populate-DropDownList-based-on-another-using-C-and-VBNet-in-ASPNet/Wed, 04 Sep 2019 02:41:23 GMT