Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

Populate combobox using MySQL .Net connector

Experts,

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


        conn1.Open()
        adapter1.Fill(dSet1, "table1")
        conn1.Close()

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

TIA,

crafuse

0
crafuse
Asked:
crafuse
  • 3
  • 3
2 Solutions
 
SStoryCommented:
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)
     me.name=name
     me.id=id
  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
0
 
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.

Thanx.



0
 
SStoryCommented:
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.

SELECT ID,NAME FROM TABLE1
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.


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
crafuseAuthor 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

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)

                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)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
0
 
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.
0
 
SStoryCommented:
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.
0
 
NetminderCommented:
Starting autoclose for asker.

Netminder
Senior Admin
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now