6 Replies - 403 Views - Last Post: 06 August 2019 - 08:10 PM Rate Topic: -----

#1 enigma04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 19

Populate one combobox from another using MySql Tables

Posted 02 August 2019 - 11:05 AM

Hello Everyone,

I am a new member here but have used the forums for information often. I have searched extensively for the information I need but can't find the answers I'm looking for. It may be a matter of just not using the right keywords so maybe somewhere here can point me in the right direction.I am fairly familiar with VB but am new to SQL/MySQL.

I have a windows form application. One of the forms contains two comboboxes. Combobox1 is populated from table 1 of a MySql database. (which is working properly) My problem is, I need to populate combobox2 with information from table 2 but based on the selection of combobox1. Everything I have works perfectly up to this point, however, I have no idea where to begin on the code for the next combobox. I have established a foreign key for to link the tables together.

Could someone please help me to understand what I may need to search for or what direction I should go in.
I can post my existing code if necessary, but I think it is irrelevant to problem.

Thanks to everyone in advance!

Kevin

Is This A Good Question/Topic? 0
  • +

Replies To: Populate one combobox from another using MySql Tables

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,209
  • Joined: 12-January 10

Re: Populate one combobox from another using MySql Tables

Posted 02 August 2019 - 11:10 AM

this might help you

https://docs.microso...etframework-4.8

then you will have to do some code for when the selected item is changed and call your db to fill that combo box
Was This Post Helpful? 0
  • +
  • -

#3 enigma04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 19

Re: Populate one combobox from another using MySql Tables

Posted 02 August 2019 - 12:33 PM

This is my current code. I understand the selected item change. It all works without error, however, the second combobox is filled with ALL data from my second table. I would like to filter it based on my first combobox selection. I just don't know how to make the second combobox read from my second table based on the selection. Is there a binding function I should use. It may be simpler then I'm thinking it should be.

Imports MySql.Data.MySqlClient
Public Class Prints

    Dim CN As New MySqlConnection
    Dim command As MySqlCommand
    Private Sub Prints_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.WindowState = FormWindowState.Maximized

        Dim reader As MySqlDataReader
        CN.ConnectionString = "server=localhost; userid=root; [email protected]; database=mysql;"

        Try
            CN.Open()
            Dim query As String
            query = "select * from parts.customers"
            command = New MySqlCommand(query, CN)
            reader = Command.ExecuteReader

            While reader.Read
                Dim customer_name = reader.GetString("Customer Name")
                Customerbox.Items.Add(customer_name)

            End While

            CN.Close()



        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CN.Dispose()
        End Try
    End Sub

    Private Sub Customerbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Customerbox.SelectedIndexChanged

        Dim reader As MySqlDataReader
        CN.ConnectionString = "server=localhost; userid=root; [email protected]; database=mysql;"

        Try
            CN.Open()
            Dim query As String
            query = "select * from parts.parts"
            command = New MySqlCommand(query, CN)
            reader = command.ExecuteReader


            While reader.Read
                Dim description = reader.GetString("Description")
                Partsbox.Items.Add(description)

            End While

            CN.Close()



        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CN.Dispose()
        End Try

    End Sub
End Class



Thank You,
Kevin
Was This Post Helpful? 0
  • +
  • -

#4 enigma04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 19

Re: Populate one combobox from another using MySql Tables

Posted 03 August 2019 - 06:48 PM

I got it!!!
To anyone facing a similar problem here is my updated code:

Imports MySql.Data.MySqlClient
Public Class Prints

    Dim CN As New MySqlConnection
    Dim command As MySqlCommand
    Private Sub Prints_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.WindowState = FormWindowState.Maximized

        Dim reader As MySqlDataReader
        CN.ConnectionString = "server=localhost; userid=root; [email protected]; database=mysql;"

        Try
            CN.Open()
            Dim query As String
            query = "select DISTINCT Customer_name from parts.customers"
            command = New MySqlCommand(query, CN)
            reader = Command.ExecuteReader

            While reader.Read
                Dim customername = reader.GetString("Customer_Name")
                Customerbox.Items.Add(customername)

            End While

            CN.Close()



        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CN.Dispose()
        End Try
    End Sub

    Private Sub Customerbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Customerbox.SelectedIndexChanged

        Partsbox.Items.Clear()

        Dim reader As MySqlDataReader
        CN.ConnectionString = "server=localhost; userid=root; [email protected]; database=mysql;"

        Try
            CN.Open()
            Dim query As String
            query = "select * from parts.parts where Customer_name = '" & Customerbox.Text & "'"
            command = New MySqlCommand(query, CN)
            reader = command.ExecuteReader


            While reader.Read
                Dim description = reader.GetString("Description")
                Partsbox.Items.Add(description)


            End While

            CN.Close()



        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CN.Dispose()
        End Try

    End Sub
End Class


As I expected, it was very simple. I just didn't know exactly how to describe what I needed. One search lead to another, to another, to another.... End result, my query for the second combobox was the issue. I just had not learned enough about mySql queries yet. Anyway, I hope this helps someone else in some way or another.

Kevin
Was This Post Helpful? 0
  • +
  • -

#5 enigma04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 19

Re: Populate one combobox from another using MySql Tables

Posted 03 August 2019 - 06:56 PM

Is there no option to mark post as [Resolved]? I seem to find it, or a way to edit my post title. However, I may be overlooking it.
Was This Post Helpful? 0
  • +
  • -

#6 enigma04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 19

Re: Populate one combobox from another using MySql Tables

Posted 06 August 2019 - 07:20 PM

WOW!!!
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,182
  • Joined: 12-June 08

Re: Populate one combobox from another using MySql Tables

Posted 06 August 2019 - 08:10 PM

Unfortunately, no. Though your post indicating what you fix does well enough! Great job.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1