Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting below error when i bound columns in the menuitems.

"Index must be within the bounds of the List.
Parameter name: index"

See my code

Dim mnuItems As New MenuItem()
       Conn.Open()
       StrQry = ""
       StrQry = " Select isnull(Adm,'') as Adm,isnull(Home,'') as Home,"
       StrQry &= " isnull(Note,'') as Notes from tab1"
       Cmd.Connection = Conn
       Cmd.CommandText = StrQry
       Rdr = Cmd.ExecuteReader
       If Rdr.HasRows Then
           While Rdr.Read
              If Rdr("Home") = "Y" Then
                   mnuItems.NavigateUrl = "~\Home.aspx"
                     mnuItems.Text = "Home"
                   NavigationMenu.Items.Add(mnuItems)
              End If
               If Rdr("Adm") = "Y" Then
                   mnuItems.NavigateUrl = "~\Adm.aspx"
                   mnuItems.Text = "Adm"
                    NavigationMenu.Items.Add(mnuItems)

               End If
           End While
       End If
       Rdr.Close()
       Conn.Close()
   End Sub

Error getting in the Second Rdr("Adm") --> NavigationMenu.Items.Add(mnuItems)

What is the problem in the second menu items when adding...?
Posted
Updated 2-Aug-11 2:46am
v4

C#
While Rdr.Read
                        NavigationMenu.Items.Clear()

you always clear your menu!

and if your code
VB
If Rdr("Home") = "Y" Then
                             mnuItems.NavigateUrl = "~\Home.aspx"
                             mnuItems.Text = "Home"
                             NavigationMenu.Items.Add(mnuItems)
                       Else
                             NavigationMenu.Items.Remove(mnuItems)

is not Y then you wnat to delete a non existing menuitem.

Better do a debug and see in what line the error comes up.
 
Share this answer
 
v2
Comments
gani7787 2-Aug-11 5:44am    
If rdr("Home") = "Y" then
'''Adding the Menu
Else
'''I don't want to display the menu
Herman<T>.Instance 2-Aug-11 5:51am    
you don't want to display the menu or the menuitem?
gani7787 2-Aug-11 6:06am    
sorry..
I should't display the menuitem...
gani7787 2-Aug-11 7:18am    
menuitem...
gani7787 2-Aug-11 7:39am    
any help pls...
You are removing mnuItems from NavigationMenu.Items if Home or Admin are not equal to Y but nothing has been added to the menu, so you are getting an index error - the Else blocks are redundant
 
Share this answer
 
VB
NavigationMenu.Items.Clear()
Conn.Open()
StrQry = ""
StrQry = " Select isnull(Admin,'') as Admin,isnull(Home,'') as Home,"
StrQry &= " isnull(Note,'') as Notes from tab1"
Cmd.Connection = Conn
Cmd.CommandText = StrQry
Rdr = Cmd.ExecuteReader
If Rdr.HasRows Then
      While Rdr.Read
            If Rdr("Home") = "Y" Then
                  Dim mnuItems As New MenuItem()
                  mnuItems.NavigateUrl = "~\Home.aspx"
                  mnuItems.Text = "Home"
                  NavigationMenu.Items.Add(mnuItems)
            Else If Rdr("Admin") = "Y" Then
                  Dim mnuItems As New MenuItem()
                  mnuItems.NavigateUrl = "~\Admin.aspx"
                  mnuItems.Text = "Admin"
                  NavigationMenu.Items.Add(mnuItems)
            End If
      End While
End If
Rdr.Close()
Conn.Close()
End Sub
 
Share this answer
 
Comments
gani7787 2-Aug-11 8:30am    
Superb...thanks...it's working....
Can you give me one more idea..?
How to highlight the particular tab in diff. color..?
Herman<T>.Instance 2-Aug-11 8:45am    
there are several css classes you can set in the Menu component. Create in your css files the code for this and you the css name class in the cssclass properties of the Menu component.

Please select that this question is answered

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