Solved

Populate combobox using MySQL .Net connector

Posted on 2011-09-06
8
494 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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