Hello everyone!
Today we are going to go over the steps to pull information from your MySql database and fill a combobox with specific information.
First you will need to go check this post out before being able to follow through with this process.
https://steemit.com/vb/@croftj0827/linking-mysql-to-your-vb-net-project
That post will show you how to connect your MySql database to your VB.Net project.
Now that you have connected your MySql database to your VB.Net project...
LETS GET STARTED!
You want to start off by creating a new MySql Connection, MySql Connection String and MySql Data Reader.
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=B3ll2424;database=ttpos"
Dim READER As MySqlDataReader
Next you we will use a Try/Catch.
Try
Catch ex As Exception
End Try
Inside the 'Try' area we want to open the MySql Connection then create a variable for the MySql query we will be sending to the database.
Next we will run our MySql command and pull the correct information from the database. We then fill the combobox with the selected data.
We then close the MySql connection.
Here is an example of the 'Try' area...
Try
MysqlConn.Open()
Dim Query As String
Query = "Select * From `employees`"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim sName = READER.GetString("PfirstName")
ComboBox2.Items.Add(sName)
End While
MysqlConn.Close()
You then just need to finish up by popping up a message box in the 'Catch' area to show any errors.
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally...
We will dispose of the MySql Connection.
There you go you have successfully filled a combobox with data from your MySql database.
Below is the entire code needed for this process.
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=YOURDATABASEUSERID;password=YOURDATABASEPASSWORD;database=YOURDATABASENAME"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "Select * From `DATABASETABLE`"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim sName = READER.GetString("DATABASECOLUMNNAME")
ComboBox2.Items.Add(sName)
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
If this did not work for you then leave a comment, we will get it figured out ;)
Much Love Steemit!
Congratulations @croftj0827! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes received
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP