For each category, display its subcategory

I am kind of new to SQL and and am trying to create a forum application. I have created two tables. And I am selecting the data in the tables using this query:

    forum_categories.CategoryName, forum_subcategories.SubCategoryName  
    forum_subcategories ON forum_categories.CategoryId = forum_subcategories.CategoryId

enter image description here

  • Parse XML with multilevel nesting in SQL
  • SQL Server 2005:charindex starting from the end
  • Add stored procedure to Entity Framework
  • How do I rewrite this SQL query in LINQ format?
  • Right Trimming Binary Data in SQL Server
  • Join two queries
  • This is what the query returns:

    enter image description here

    Now what I need is for each CategoryName I need to display it’s subcategories. I am using a ListView for this:

    <asp:ListView ID="ListView1" runat="server" DataSourceID="ForumDataSource">
            <div id='<%#Eval("CategoryName") %>' class="PostCategories">
                <h2><asp:Label ID="Label1" runat="server" Text='<%#Eval("CategoryName") %>'></asp:Label></h2>
                <table class="ForumPosts">
                        <td>Last Post</td>
                            <td><asp:Label ID="Label2" runat="server" Text='<%#Eval("SubCategoryName") %>'></asp:Label></td>
            <p>No Data Found</p>
    <asp:SqlDataSource ID="ForumDataSource" runat="server" 
        ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" 
        SelectCommand="SELECT forum_categories.CategoryName, forum_subcategories.SubCategoryName FROM forum_categories  LEFT JOIN forum_subcategories ON  forum_subcategories.CategoryId = forum_categories.CategoryId">

    Now as everyone might have already understood from the code this returns 8 tables.

    What I would like to get is for each category to display it’s subcategory data.In this case there should be only 3 tables

    How should I approach this problem?

    Should this be solved using SQL or manipulating the received data with a server side language?

  • Call a stored procedure with parameter in c#
  • How can I solve a connection pool problem between ASP.NET and SQL Server?
  • Complex editing on a gridview in C#
  • Entity framework very slow to load for first time
  • Entity Framework MVC Slow Page Loads
  • How to get specific result from a search in a SQL column populated with XML
  • One Solution collect form web for “For each category, display its subcategory”

    I’m not a big fan of ListViews. But you can try this out,

    You need two list views. Outer listview and inner listview.

    Outer one will loop through the categories hence it should give you only three tables. Make sure you select only the distinct ones.

    Then inner listview will loop through each CategoryID and fetch the sub categories linked to CategoryID.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.