Solved

Populate combobox using MySQL .Net connector

Posted on 2011-09-06
8
500 Views
Last Modified: 2012-08-14
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
Comment
Question by:crafuse
  • 3
  • 3
8 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 300 total points
ID: 36488707
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
 

Author Comment

by:crafuse
ID: 36488798
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
 
LVL 25

Expert Comment

by:SStory
ID: 36490633
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Accepted Solution

by:
crafuse earned 0 total points
ID: 36493152
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
 

Author Comment

by:crafuse
ID: 36495137
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
 
LVL 25

Expert Comment

by:SStory
ID: 36495138
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
 
LVL 5

Expert Comment

by:Netminder
ID: 36534739
Starting autoclose for asker.

Netminder
Senior Admin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question