Link to home
Start Free TrialLog in
Avatar of jay-are
jay-areFlag for United States of America

asked on

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?
Avatar of guru_sami
guru_sami
Flag of United States of America image

Check this example: Populate menu from database

You might also want to look into Custom SitemapProvider.
Avatar of jay-are

ASKER

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.
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

Avatar of jay-are

ASKER

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.
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

Avatar of jay-are

ASKER

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?
Avatar of jay-are

ASKER

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.?
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay-are

ASKER

Ah that's perfect.  I knew someone here could get this right.  Thank you so much!
Avatar of jay-are

ASKER

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