1.11M Members

Retrieving Data from MySQL Database to ComboBox

 
0
 

Hello, I have made an Employee Time clock application. I have been working on connecting my application to a MySQL database, I have a textbox wich is used to add an employee name to my database (this is working perfectly.), but I also a combobox where I want the list of Employee names retrieved from the database (this is where my problem is.)
I have looked at a few different forum posts, and I still can't seem to get it to work. With this method it won't connect to my Database, but when I take out this code out everything else connects and works just fine.
Some help would be much appreciated!
Here's my code:

Imports MySql.Data.MySqlClient

Public Class MainForm
    Dim str As String
    Dim strSQL As String = "SELECT distinct ID, Employee FROM employees"
    Dim da As New MySqlDataAdapter(strSQL, conn)
    Dim ds As New DataSet
    Dim conn As New MySqlConnection
    Dim MyCmd As New MySqlCommand("SELECT * FROM users", conn)


    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try
            str = " host = localhost ; username = 'username'; password = 'password'; database= vb_timeclock; pooling = false;"
            conn.ConnectionString = str
            conn.Open()

            da.Fill(ds, "employees")
            With (cmbEmployee)

                .Items.Add("Select")
                .DataSource = ds.Tables("emplyees")
                .DisplayMember = "Employee"
                .ValueMember = "ID"
                .SelectedIndex = 0

            End With

        Catch ex As Exception
            MessageBox.Show("Problem connecting to databese!")

        End Try
 
0
 

You forgot to call the .DataBind() method of your combobox. Without this it won't show the itens in the select.

I made a few changes in your code, try it like this:

Imports MySql.Data.MySqlClient

Public Class MainForm


    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try

            Dim conn As New MySqlConnection
            Dim str As String = " host = localhost ; username = 'username'; password = 'password'; database= vb_timeclock; pooling = false;"
            conn.ConnectionString = str
            conn.Open()

            Dim strSQL As String = "SELECT distinct ID, Employee FROM employees"
            Dim da As New MySqlDataAdapter(strSQL, conn)
            Dim ds As New DataSet

            da.Fill(ds, "employees")
            With (cmbEmployee)

                .Items.Add("Select")
                .DataSource = ds.Tables("emplyees")
                .DisplayMember = "Employee"
                .ValueMember = "ID"
                .SelectedIndex = 0
                .DataBind()

            End With

        Catch ex As Exception
            ' Its nice to show the message so you know what happened
            MessageBox.Show("Problem connecting to databese: " & ex.Message)

        End Try
 
0
 

You may also like to look at this

Combobox

 
0
 

@ AleMonteiro Oh Thank You Thank You Thank You!!!

With the tweaks you suggested everything is working perfectly now! Couldn't ask for better. Thank you very very much. :D

Question Answered as of 1 Year Ago by AleMonteiro and Lethugs
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: