ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Saving and Filling Data in the ComboBox in VB.Net And MySQL Database

For today, I will teach you how to store and fill the data in the ComboBox in VB.Net and MySQL database. With this, you can save the data and it will automatically fill in the ComboBox.

So, let’s begin.

First, create a database in the MySQL database and name it “dbmember”. After that, create a table in it and name it “member”

  1. CREATE TABLE IF NOT EXISTS `member` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `name` text NOT NULL,
  4.   PRIMARY KEY (`id`)

Open the Visual Basic and create a new Windows Form Application. Set the Form Just like this.

datastoresaving

After setting up the Form, double click it and set the connection of MySQL Database to Visual Basic. After that, Declare all the classes that are needed above the Form1_Load.

  1. 'SET THE CONNECTION BETWEEN VISUAL BASIC AND MYSQL DATABASE  
  2.     Dim con As MySqlConnection = New MySqlConnection("server=localhost; user id=root; password=; database=dbmember")
  3.     'A SET OF COMMAND IN MYSQL
  4.     Dim cmd As New MySqlCommand
  5.     'SET A CLASS THAT SERVE THE BRIDGE BETWEEN A DATASET AND DATABASE FOR SAVING AND RETRIEVING DATA.
  6.     Dim da As New MySqlDataAdapter
  7.     'SET A CLASS THAT CONSIST A COMPLETE SET OF DATA
  8.     Dim ds As New DataSet

Now, in the Form_load, set up a code for filling the ComboBox on the first load of the Form.

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'OPENING THE CONNECTION
  3.         con.Open()
  4.         'SET A QUERY FOR RETRIEVING DATA
  5.         Dim sqlquery As String = "select * from member"
  6.         'IT HOLDS THE DATA TO BE EXECUTED
  7.         With cmd
  8.             .Connection = con
  9.             .CommandText = sqlquery
  10.         End With
  11.         'FILLING THE DATA IN THE TABLE
  12.         da.SelectCommand = cmd
  13.         ds = New DataSet
  14.         da.Fill(ds, "member")
  15.         'GETTING THE DATASOURCE OF THE COMBOBOX
  16.         ComboBox1.DataSource = ds.Tables(0)
  17.         'SET THE FIELD IN THE TABLE TO BE DISPLAY IN THE COMBOBOX
  18.         ComboBox1.DisplayMember = "name"
  19.         'CLOSING THE CONNECTION
  20.         con.Close()
  21.         da.Dispose()
  22.     End Sub

After setting up the code for filling ComboBox, go back to the Form Design and double click the Save Button. After that, you have to put a code for saving the data in the database and fill it into the ComboBox.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Try
  3.             con.Open()
  4.             'DECLARE A VARIABLE AS THE RESULT
  5.             Dim result As Integer
  6.             'SET A QUERY FOR SAVING DATA
  7.             Dim sqlQuery As String = "INSERT INTO member (name) values ('" & TextBox1.Text & "')"
  8.             'IT HOLDS THE DATA TO BE EXECUTED
  9.             With cmd
  10.                 .Connection = con
  11.                 .CommandText = sqlQuery
  12.             End With
  13.             'IT EXECUTE THE DATA
  14.             result = cmd.ExecuteNonQuery
  15.             'IT NOTIFY IF THE DATA IS SAVE OR NOT.
  16.             If result > 0 Then
  17.                 MsgBox("Data has been save in the database")
  18.             Else
  19.                 MsgBox("Failed to save the data in the database")
  20.             End If
  21.  
  22.         Catch ex As Exception
  23.             MsgBox(ex.Message)
  24.         End Try
  25.         con.Close()
  26.         'CALL THE FIRST LOAD OF THE FORM TO REFRESH THE DATA IN THE COMBOBOX
  27.         Call Form1_Load(sender, e)
  28.     End Sub

Complete Source Code is included. Download and run it on your computer.
DOWNLOAD HERE

Facebook Comments

Check Your Domain Ranking

1 Comment

Add a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!