Solved

Populate combobox using MySQL .Net connector

Posted on 2011-09-06
8
485 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now