For Next Loop - Nested?

Hello Experts,

I'm trying to add items to an asp.net menu in code-behind using a for loop.  The dataset I've filled contains a column called "MenuHeader".  I want to loop through each MenuHeader and add that MenuHeader as the first item, then add a child item for each "MenuChildName" from that same dataset.

My data looks like this:

MenuHeader     MenuChildName
"Header1"         "dog"
"Header1"         "cat"
"Header2"         "bird"
"Header3"         "fish"

So I want a parent menu item for each Header#, and their children to be the MenuChildName.  I thought this would require a nested loop but I'm not even sure how to loop through each MenuHeader properly.

Dim dsMenuItems As DataSet = New DataSet
Dim ssMenuItems = "select itemid, reportname, menuchildname, reportpath, creationdate, modifieddate, useraccess, menuorder, menuheader from catalog where menuname is not null and useraccess like '%" & usertype & "%'"
Dim AdapterMenuItems As SqlDataAdapter = New SqlDataAdapter
Dim ConnectionStringMenuItems As String = System.Configuration.ConfigurationManager.ConnectionStrings("dopsCat").ConnectionString()
Dim connMenuItems As New SqlConnection(ConnectionStringMenuItems)
AdapterMenuItems = New SqlDataAdapter(ssMenuItems, ConnectionStringMenuItems)
connMenuItems.Open()
AdapterMenuItems.Fill(dsMenuItems, "catalog")
        If Not dsMenuItems.Tables("catalog").Rows.Count = 0 Then

            For Each dsMenuItems.Tables("catalog").Rows.Item("MenuHeader") In dsMenuItems
                Menu1.Items.Add(New MenuItem(MeanuHeaderColumnValue)
                'another loop to add that MenuHeader's MenuChildName?
            Next


        End If
        connMenuItems.Close()

Open in new window


Will someone give me some help on building this menu properly?
jay-areAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
Check this example: Populate menu from database

You might also want to look into Custom SitemapProvider.
0
jay-areAuthor Commented:
I think this can be handled with a for loop, I'm just not good enough to figure it out on my own.  I wasn't sure if I would need a loop for each of the possible menu headers.
0
guru_samiCommented:
Sorry may be I interpreted it wrong. Try this:

For Each dsMenuItems.Tables("catalog").Rows.Item("MenuHeader") In dsMenuItems
        Dim item As MenuItem = New MenuItem(MeanuHeaderColumnValue)
        item.ChildItems.Add(New MenuItem(MenuChildName))
        Menu1.Items.Add(item)
Next

Open in new window

0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

jay-areAuthor Commented:
That looks more like what I need.  The error I get from this code is the same thing I am running into when I try this on my own.  

For Each dsMenuItems.Tables("catalog").Rows.Item("MenuHeader") In dsMenuItems

Open in new window


"In dsMenuItems"  -  Error: Expression is of the type System.Data.Dataset, which is not a collection type.
0
guru_samiCommented:
Try this:
For Each row As DataRow In dsMenuItems.Tables("catalog").Rows
    Dim item As MenuItem = New MenuItem(row("MenuHeader").ToString)
    item.ChildItems.Add(New MenuItem(row("MenuChildName").ToString))
    menuAnimals.Items.Add(item)
Next

Open in new window

0
jay-areAuthor Commented:
That populates the menu!  My fear was that it would have repetitive MenuHeaders.  If you look at the data example I posted, I have multiple child items for each MenuHeader.  The menu will post each "Header1".  In this case I have 2 Header1s instead of just one with both child items.  How do I consolidate these using the loop?
0
jay-areAuthor Commented:
Perhaps, since I know that my MenuHeaders will always be 1 of 8 options, the loop should be run for each of the MenuHeader types?  A for loop for Header1, then for Header2, etc.?
0
guru_samiCommented:
Try this:
For Each row As DataRow In dsMenuItems.Tables("catalog").Rows
Dim item As MenuItem = Nothing
item = Menu1.FindItem(row("MenuHeader").ToString)
If (item Is Nothing) Then
    item = New MenuItem(row("MenuHeader").ToString)
    item.ChildItems.Add(New MenuItem(row("MenuChildName").ToString))
    Menu1.Items.Add(item)
Else
    item.ChildItems.Add(New MenuItem(row("MenuChildName").ToString))
End If
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jay-areAuthor Commented:
Ah that's perfect.  I knew someone here could get this right.  Thank you so much!
0
jay-areAuthor Commented:
Guru_sami,

If you are still listening to this question I have posted a new question.  I need help using this same process to create tables instead of an asp.net menu.  Please help!
For Each Loop - Create Table
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.