zack
zack

Reputation: 11

Store Database column to an array on buttons using vb.net

I want to select ItemName and ItemPrice from my Database MySql and then store the ItemNames and ItemPrices into a buttons so when i click a button i get the name and price.

Here is my code and its currently working and only displaying the price on Row in the database.I think i need to make by button to be an array for that to work.

It would be great if someone help me. I am new to vb.net and would like to fix this program

Public Property BtnMenuArray As Button
Public Property BtnDoneArray As Button
Public Property GBCustemerArray As GroupBox
Public Property TBCustemerOrderArray As TextBox
Public Property TBMenuItemNameArray As TextBox
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset    
Public Function GetItemPrice(selection) As String
    TBMenuItemNameArray = New TextBox
    Dim output As String
    con.Open("Dsn=Invintory;uid=root")
    rs.Open("Select ItemPrice From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemName(selection) As String
    Dim output As String
    con.Open("Dsn=Invintory;uid=root")
    'rs.Open("Select ItemName From menuitem", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    rs.Open("Select ItemName From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    'MsgBox("Detailes are Added")
    Return output
End Function
Public Sub RunDB()
    Dim Ittrat As Short = GetItemCount()
    TextBox1.Text = Ittrat
    Dim MyNameArray As New List(Of String)
    Dim MyItemPriceArray As New List(Of String)
    For item = 0 To Ittrat - 1
        BtnMenuArray = New Button()
        MyNameArray.Add(GetItemName(item))
        BtnMenuArray.Text = MyNameArray(item)
        TableLayoutPanel1.Controls.Add(BtnMenuArray)
        MyItemPriceArray.Add(GetItemPrice(item))
        BtnMenuArray.Tag = MyItemPriceArray(item)
    Next
    AddHandler BtnMenuArray.Click, AddressOf ClickHandle
End Sub
Public Sub ClickHandle(ByVal sender As Object, ByVal e As _
   System.EventArgs)
    MsgBox(BtnMenuArray.Tag)
End Sub

Upvotes: 0

Views: 81

Answers (1)

zack
zack

Reputation: 11

I was able to figure it out. What i needed to do is add the name of the button inside the for loop i was using to ittrat to assign a name of the buttons as an array. Also this fixed my problem with the dynamic button event handler. Now i am able to see a name,text, and tag of each button when the button is clicked.

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

Public Property ItemMenuBT As Button
Public Property BtnDoneArray As Button
Public Property GBCustemerArray As GroupBox
Public Property TBCustemerOrderArray As TextBox
Public Property TBMenuItemNameArray As TextBox

Public Function GetItemCount() As Short
    TBMenuItemNameArray = New TextBox
    Dim output As Short
    con.Open("Connection String")
    rs.Open("Select Count(RowID) From TableNameHere", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = Convert.ToInt16(rs.GetString())
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemPrice(selection) As String
    TBMenuItemNameArray = New TextBox
    TextBox1.Text = selection
    Dim output As String
    con.Open("Connection String")
    rs.Open("Select ColumnName From TableName where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemName(selection) As String
    Dim output As String
    con.Open("Connection String")
    rs.Open("Select ItemName From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Sub LOAD_ITEM_MENU()
    Dim Ittrat As Short = GetItemCount()
    Dim MyNameArray As New List(Of String)
    Dim MyItemPriceArray As New List(Of String)
    Dim buttonName As String
    Dim buttonText As String
    For item = 0 To Ittrat - 1
        ItemMenuBT = New Button()
        ItemMenuBT.Size = New Size(130, 100)
        buttonName = GetItemName(item)
        buttonText = GetItemPrice(item)
        ItemMenuBT.Name = buttonName
        ItemMenuBT.Text = buttonName
        ItemMenuBT.Tag = buttonText
        TableLayoutPanel1.Controls.Add(ItemMenuBT)
        AddHandler ItemMenuBT.Click, AddressOf Me.ClickHand
    Next
End Sub
Public Sub ClickHand(ByVal sender As Object, ByVal e As _
   System.EventArgs)
    Dim CST_ORD_SELCTEDItemName As Button = sender
    'MsgBox("Item Name: " & CST_ORD_SELCTEDItemName.Name)
    'MsgBox("Item Price: " & CST_ORD_SELCTEDItemName.Tag)
    Dim valueNew As Double
    valueNew = CST_ORD_SELCTEDItemName.Tag
    TBCustemerOrderArray.Text = Val(TBCustemerOrderArray.Text) + valueNew
End Sub

Upvotes: 0

Related Questions