Retrieving Data from MySQL Database to ComboBox
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
Related Article: MySQL database visual basic 2010 queries
is a VB.NET discussion thread by beforetheyknew that has 1 reply, was last updated 1 year ago and has been tagged with the keywords: mysql, database, visual, basic.
ScarWars9
Light Poster
40 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 25
You may also like to look at this
Combobox
Lethugs
Junior Poster
102 posts since Nov 2012
Reputation Points: 8
Solved Threads: 7
Skill Endorsements: 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
ScarWars9
Light Poster
40 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by
AleMonteiro
and
Lethugs