Click here to Skip to main content
15,909,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How menus save in data base as parent menu and as sub menus, how we create relationship between parent menus to sub menus in vb.net
any one help me plz
Posted

Hi,

Understand the following sample and try yourself :thumbsup:

VB
Private Sub PopulateMenu()
    Dim ds As DataSet = GetDataSetForMenu()

    Dim menu As New Menu()

    For Each parentItem As DataRow In ds.Tables("Categories").Rows
        Dim categoryItem As New MenuItem(DirectCast(parentItem("CategoryName"), String))
        menu.Items.Add(categoryItem)

        For Each childItem As DataRow In parentItem.GetChildRows("Children")
            Dim childrenItem As New MenuItem(DirectCast(childItem("ProductName"), String))
            categoryItem.ChildItems.Add(childrenItem)
        Next
    Next

    Panel1.Controls.Add(menu)
    Panel1.DataBind()

End Sub

Private Function GetDataSetForMenu() As DataSet
    Dim myConnection As New SqlConnection(GetConnectionString())
    Dim adCat As New SqlDataAdapter("SELECT * FROM Categories", myConnection)
    Dim adProd As New SqlDataAdapter("SELECT * FROM Products", myConnection)

    Dim ds As New DataSet()
    adCat.Fill(ds, "Categories")
    adProd.Fill(ds, "Products")

    ds.Relations.Add("Children", ds.Tables("Categories").Columns("CategoryID"), ds.Tables("Products").Columns("CategoryID"))
    Return ds

End Function


[Modified: if you're going to provide code, learn to use the pre tags...their your friend!]
 
Share this answer
 
v2
I'm guessing that you have a template and you want to use that template for multiple pages and each page can have different menus.

The easiest way to do that is with one table. It doesn't have to be a fancy table either. It could look like this:

Column 1: Page
Column 2: Item Name
Column 3: Item ID
Column 4: Item Link
Column 5: Parent ID
Column 6: Order

For each root menu, you would just set parent either to null or something like "Root".

Then, when you build the page in the code, you look up all of the Root items for that page first, then add them to the menu in the specified order. As you add each item, check to see if it is a parent to any other items and add them.

You could do it with multiple tables and setting up relationships, but then you have to have a separate table for each new level and if you add a page with more levels, then you have to add more tables.
 
Share this answer
 
Comments
Prakash.LE 24-Jun-10 2:11am    
Hi William, Thanks for your suggestion on posting code by using pre tag.

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