locked
Menu values to be retieved from database in VB.NET (VS 2017) RRS feed

  • Question

  • User-1578974752 posted

    I have menu in the Home page.TEST

    Under TEST have 3 values. I want the 3 values under TEST menu to be picked from the database.Appreciate the help

     <asp:Menu ID="Menu1" runat="server" CssClass="auto-style144" Orientation="Horizontal" RenderingMode="Table" Font-Bold="True" Height="17px" Width="663px">
                    <Items>
                        <asp:MenuItem Text="HOME" Value="HOME">
                            <asp:MenuItem></asp:MenuItem>
                        </asp:MenuItem>
                            <asp:MenuItem Text="TEST" Value="TEST">
                            <asp:MenuItem Text="Instructions" Value="Instructions"></asp:MenuItem>
                            <asp:MenuItem NavigateUrl="~/TestIT.aspx" Text="IT" Value="IT"></asp:MenuItem>
                            <asp:MenuItem NavigateUrl="~/TestEngg.aspx" Text="Engg" Value="Engg"></asp:MenuItem>
                            <asp:MenuItem NavigateUrl="~/TestSol.aspx" Text="Sol" Value="Sol"></asp:MenuItem>
                        </asp:MenuItem>
                        <asp:MenuItem Text="LOGOUT" Value="LOGOUT" NavigateUrl="~/Loginpage.aspx"></asp:MenuItem>
                    </Items>
                    <StaticHoverStyle BackColor="#33CCFF" ForeColor="Maroon" />
                </asp:Menu>

    Wednesday, August 29, 2018 3:07 AM

Answers

  • User-369506445 posted

    hi

    please try below code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim subMenu = New MenuItem("Test")
            Dim items As ArrayList = fetchFromDB()
    
            For index = 0 To items.Count-1
                subMenu.ChildItems.Add(New MenuItem(items.Item(index) , "Value"))
            Next
    
            Menu1.Items.Add(subMenu) 
        End Sub
    Private Function fetchFromDB() As ArrayList
            Dim arraylist As New ArrayList
    
            Dim conn As New SqlConnection
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = ("Data Source=.;Initial Catalog=Your Database Name;trusted_connection=true")
            End If
    
            Try
                conn.Open()
                Dim sqlquery As String = "SELECT name FROM tblMenu "
                Dim data As SqlDataReader
                Dim adapter As New SqlDataAdapter
                Dim parameter As New SqlParameter
                Dim command As SqlCommand = New SqlCommand(sqlquery, conn)
    
                command.Connection = conn
                adapter.SelectCommand = command
                data = command.ExecuteReader()
                While data.Read
                    If data.HasRows = True Then
                        arraylist.Add(data(0))
                    End If
                End While
    
                Return arraylist
            Catch ex As Exception
                Return arraylist
            End Try
        End Function

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2018 5:05 AM

All replies

  • User-369506445 posted

    hi

    please try below code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim subMenu = New MenuItem("Test")
            Dim items As ArrayList = fetchFromDB()
    
            For index = 0 To items.Count-1
                subMenu.ChildItems.Add(New MenuItem(items.Item(index) , "Value"))
            Next
    
            Menu1.Items.Add(subMenu) 
        End Sub
    Private Function fetchFromDB() As ArrayList
            Dim arraylist As New ArrayList
    
            Dim conn As New SqlConnection
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = ("Data Source=.;Initial Catalog=Your Database Name;trusted_connection=true")
            End If
    
            Try
                conn.Open()
                Dim sqlquery As String = "SELECT name FROM tblMenu "
                Dim data As SqlDataReader
                Dim adapter As New SqlDataAdapter
                Dim parameter As New SqlParameter
                Dim command As SqlCommand = New SqlCommand(sqlquery, conn)
    
                command.Connection = conn
                adapter.SelectCommand = command
                data = command.ExecuteReader()
                While data.Read
                    If data.HasRows = True Then
                        arraylist.Add(data(0))
                    End If
                End While
    
                Return arraylist
            Catch ex As Exception
                Return arraylist
            End Try
        End Function

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2018 5:05 AM
  • User-1578974752 posted

    Thanks vahid bakkhi

    I have added 2 menu item as below from your code. Now both the menu clicked will go to Network.aspx. How can I update such that if TESTis clicked then will navigate to TEST.aspx .If Test have 3 item below.a,b,c. When a is clicked then need to navigate to a.aspx ,b clicked to b.aspx etc. Thanks

    Dim subMenu = New MenuItem("TEST")

    Dim items As ArrayList = fetchFromDB()

    For index = 0 To items.Count - 1

    subMenu.ChildItems.Add(New MenuItem(items.Item(index), "Value"))

    Next

    Menu1.Items.Add(subMenu)

    Dim subMenu2 = New MenuItem("Review")

    Dim items1 As ArrayList = fetchFromDBQSC()

    For index = 0 To items1.Count - 1

    subMenu2.ChildItems.Add(New MenuItem(items1.Item(index), "Value"))

    Next

    Menu1.Items.Add(subMenu2)

    Private Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick

    Response.Redirect("Network.aspx")

     

    End Sub

    Thursday, September 6, 2018 5:39 AM