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 (Bind) Nested DataList from single (one) table using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DataList ID=&#34;DataList1&#34; runat=&#34;server&#34; RepeatColumns=&#34;3&#34; RepeatDirection=&#34;Horizontal&#34; OnItemCommand=&#34;DataList1_ItemCommand&#34; OnItemDataBound=&#34;DataList1_ItemDataBound&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34;&gt; &lt;tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;lblid&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;category&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Category&#34;) %&gt;&#39; CssClass=&#34;btn-lg&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:DataList ID=&#34;DataList2&#34; runat=&#34;server&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34;&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;id&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;Bname&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Sub_category&#34;) %&gt;&#39; CssClass=&#34;btn-lg&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; CommandArgument=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39; CommandName=&#34;viewdetails&#34; CssClass=&#34;btn btn-warning&#34; Font-Bold=&#34;true&#34; Text=&#34;View Details&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = "SELECT DISTINCT Category,Sub_category,Id FROM Category_tbl"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = GetData(query, " "); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList1.DataBind(); &nbsp;&nbsp;&nbsp; } } private static DataTable GetData(string query, string category) { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(query, con); &nbsp;&nbsp;&nbsp; if (!string.IsNullOrEmpty(category)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@category", category); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd); &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp; return dt; } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { &nbsp;&nbsp;&nbsp; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataRowView dataRowView = e.Item.DataItem as DataRowView; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string category = dataRowView["Category"].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataList subDataList = e.Item.FindControl("DataList2") as DataList; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = "SELECT DISTINCT Category,Sub_category,Id FROM Category_tbl WHERE Category = @category"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = GetData(query, category); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; subDataList.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; subDataList.DataBind(); &nbsp;&nbsp;&nbsp; } } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { &nbsp;&nbsp;&nbsp; if (e.CommandName == "viewdetails") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + "Id is : " + e.CommandArgument.ToString() + "')", true); &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim query As String = &#34;SELECT DISTINCT Category,Sub_category,Id FROM Category_tbl&#34; Dim dt As DataTable = GetData(query, &#34; &#34;) DataList1.DataSource = dt DataList1.DataBind() End If End Sub Private Shared Function GetData(ByVal query As String, ByVal category As String) As DataTable Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(query, con) If Not String.IsNullOrEmpty(category) Then cmd.Parameters.AddWithValue(&#34;@category&#34;, category) End If Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) Return dt End Function Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then Dim dataRowView As DataRowView = TryCast(e.Item.DataItem, DataRowView) Dim category As String = dataRowView(&#34;Category&#34;).ToString() Dim subDataList As DataList = TryCast(e.Item.FindControl(&#34;DataList2&#34;), DataList) Dim query As String = &#34;SELECT DISTINCT Category,Sub_category,Id FROM Category_tbl WHERE Category = @category&#34; Dim dt As DataTable = GetData(query, category) subDataList.DataSource = dt subDataList.DataBind() End If End Sub Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) If e.CommandName = &#34;viewdetails&#34; Then ClientScript.RegisterClientScriptBlock(Me.[GetType](), &#34;&#34;, &#34;alert(&#39;&#34; &amp; &#34;Id is : &#34; &amp; e.CommandArgument.ToString() &amp; &#34;&#39;)&#34;, True) End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/MBzSW0H.gif" alt="" width="345" height="355" /></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/185905/Populate-Bind-Nested-DataList-from-single-one-table-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/185905/Populate-Bind-Nested-DataList-from-single-one-table-using-C-and-VBNet-in-ASPNet/Thu, 05 Sep 2019 01:27:22 GMTPopulate (Bind) Nested DataList from single (one) table using C# and VB.Net in ASP.Net<blockquote><cite>itsme says:</cite> <pre><code class="csharp spaces"></code><code class="csharp plain">SqlCommand cmd = </code><code class="csharp keyword">new</code> <code class="csharp plain">SqlCommand(</code><code class="csharp string">"select Distinct Category,Sub_category,Id from Category_tbl"</code><code class="csharp plain">, con);</code></pre> </blockquote> <p>&nbsp;If you want avoid duplicate data so you need to select only category you can't select id and sub-category becase od ther having different data. refer below query.</p> <pre class="brush: csharp">SqlCommand cmd = new SqlCommand(&#34;select Distinct Category from Category_tbl&#34;, con);</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/185905/Populate-Bind-Nested-DataList-from-single-one-table-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/185905/Populate-Bind-Nested-DataList-from-single-one-table-using-C-and-VBNet-in-ASPNet/Thu, 05 Sep 2019 06:40:33 GMT