Results 1 to 3 of 3

Thread: ToolStrip Menu Item From SQL

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    52

    ToolStrip Menu Item From SQL

    Hi,

    I am wanting to populate a menu tool strip/item from a SQL table.

    Basically the table has 2 columns Link_Name & Link_Address

    The menu has already been created in designmode and i would like a Tools section and the tool strip menu item to be my Link_Name and the click event to be
    launching a browser with website link from Link_Address.

    Currently i have:

    Code:
    Dim Sqlcon As SqlConnection = New SqlConnection("Data Source=JAMESPC\SUPPORTDB;Initial Catalog=Support_DB;Persist Security Info=True;User ID=user;Password=password")
            Dim SQLcmd As SqlCommand
            Dim MenuPopulate As String = "Select * from Links "
            Dim DataAdapt As New SqlDataAdapter
            SQLcmd = New SqlCommand(MenuPopulate, Sqlcon)
            Dim ds As New DataSet
            DataAdapt.Fill(ds)
            Dim Menu As New ToolStripMenuItem
    
            If ds.Tables(0).Rows.Count = 0 Then
                Dim i As Integer
                For i As
    
                Next
    
            End If
    I am struggling on how best to write this i am aware i can fire a event handler for the ToolStripMenuItem_Click but not sure how it would talk to the SQL DB as its a seperate Private Sub.

    Thanks James

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: ToolStrip Menu Item From SQL

    You wouldn't use a data adapter and a DataTable for a start. This is a job for a data reader.
    vb.net Code:
    1. Private Sub ToolsToolStripMenuItem_DropDownOpening(sender As Object, e As EventArgs) Handles ToolsToolStripMenuItem.DropDownOpening
    2.     Dim menuItems As New List(Of ToolStripMenuItem)
    3.     Dim onClick = New EventHandler(Sub(clickSender, clickE)
    4.                                        Dim menuItem = DirectCast(clickSender, ToolStripMenuItem)
    5.  
    6.                                        Process.Start(CStr(menuItem.Tag))
    7.                                    End Sub)
    8.  
    9.     Using connection As New SqlConnection("connection string here"),
    10.         command As New SqlCommand("SELECT Link_Name, Link_Address FROM Links", connection)
    11.         connection.Open()
    12.  
    13.         Using reader = command.ExecuteReader()
    14.             While reader.Read()
    15.                 menuItems.Add(New ToolStripMenuItem(reader.GetString(reader.GetOrdinal("Link_Name")),
    16.                                                     Nothing,
    17.                                                     onClick) With {.Tag = reader.GetString(reader.GetOrdinal("Link_Address"))})
    18.             End While
    19.         End Using
    20.     End Using
    21.  
    22.     ToolsToolStripMenuItem.DropDownItems.Clear()
    23.     ToolsToolStripMenuItem.DropDownItems.AddRange(menuItems.ToArray())
    24. End Sub
    I've done that on the DropDownOpening event of the parent menu item so that the list is current every time. If that's not necessary then you can just do it once, on another event.

    The URL that corresponds to the name is stored in the Tag of the menu item. When the menu item is clicked, the event handler gets the address from the Tag and passes it to Process.Start, which will open that URL in the default browser.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    52

    Re: ToolStrip Menu Item From SQL

    Thanks jmcilhinney, that was what i was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width