Populate combobox using MySQL .Net connector


I need to populate combobox1 with the query "select id, name from table1" using MySQL .Net connector.

name = display value
id = value i need on combobox1 selected item

Please include ALL syntax, including db connection, on button click event.

Code below is T-SQL from an earlier project, more-or-less what i'm trying to do:

        Dim cmd1 As New SqlClient.SqlCommand("select id, name from table1")

        Dim adapter1 As New SqlClient.SqlDataAdapter
        adapter1.SelectCommand = cmd1
        Dim dSet1 As New DataSet

        Dim conn1 As New SqlClient.SqlConnection("Some Connection String")
        adapter1.SelectCommand.Connection = conn1

        adapter1.Fill(dSet1, "table1")

        combobox1.DataSource = dSet1.Tables("table1")
        combobox1.DisplayMember = "name"



Who is Participating?
crafuseConnect With a Mentor Author Commented:
answered myself, thanks. was looking for something like the following, which is what i asked for.

and SStory - I've been using EE since about 2001. I'm quite certain it is the place for me, thanks. But if you're giving out advice, perhaps you will take some yourself: Why not simply answer the question asked, or not at all? EE, after all, is a Q & A site.

Dim conn As New MySqlConnection
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As New DataTable
        Dim SQL As String

        SQL = "Select cardno, name from merchant"

        conn.ConnectionString = strMySQLConnectString


                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand

                cmbCardNames.DataSource = myData
                cmbCardNames.DisplayMember = "name"
                cmbCardNames.ValueMember = "cardno"
            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error connecting to the database: " & myerror.Message)
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
SStoryConnect With a Mentor Commented:
ComboBox items are of type object. They display, using ToString() method.
So just create an object... something like this:

class Person
     public ID as integer
     public Name as string

  sub new(ID as integer,Name as string)
  end sub

  (override the ToString() method) so that it has
    return me.name
end class

(all this code is typed in at this editor and not tested, but should give you the idea).

You should make property accessors instead of public member variables.

Loop through the data,

combo1.items.add(new Person(1,"John Doe"))

except 1 and "John Doe" would come from the field values.

This is an unbound way to do it.

Then when you add a db item, loop through
crafuseAuthor Commented:
SStory -

Please answer the question, or do not respond. I asked in my question:

"Please include ALL syntax, including db connection, on button click event."

I am interested in MySQL syntax, compared to T-SQL syntax.


The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

OK.  There isn't a bit of difference in the two for the query you gave.

Dim cmd1 As New SqlClient.SqlCommand("select id, name from table1")
This would be identical except you'd use the MySQL .Net connector object instead

You'd just use the corresponding MySQL objects instead of SQL Server objects.  They were written that way because each of them implements a required Interface, like ICommand, IConnection, etc.

is valid MySQL query text.

I don't know what you are talking about with button click event. What button?

If you are looking for someone to write code for you entirely, I don't think this is the place.

Much of SQL is built on a standard SQL92 was very popular. Now the various versions are moving to a newer version of that standard. This is why they both work, and this query is one of the most simple you can do.

crafuseAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for crafuse's comment http:/Q_27292946.html#36493152

for the following reason:

Answered myself.
I told the OP what to do. I didn't write the code for him. It was a simple thing of replacing SQL with MySQL as I told him. I also told him that the SQL for the query he was doing was simple and the same in either RDBMS.
Starting autoclose for asker.

Senior Admin
All Courses

From novice to tech pro — start learning today.