How to Clear ComboBox with DataSource in VB.Net and MySQL Database

This time, I will teach you how to clear ComboBox with DataSource in VB.Net and MySQL database. Removing list of records in the combo box that has a data source is a bit hard to do especially for beginners in programming. So I made this tutorial to help you clear all the data that’s inside the combo box in just a click. Below are the procedures on how to make it work.

Creating the Database

Create a database and named it "test" Then, create a table in the database that you have created.
  1. CREATE TABLE `test`.`tblrecords` ( `RecordID` INT NOT NULL AUTO_INCREMENT ,`Subject` VARCHAR(90) NOT NULL , `Course` VARCHAR(90) NOT NULL , `Category`VARCHAR(90) NOT NULL , PRIMARY KEY (`RecordID`)) ENGINE = InnoDB;

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new window form application. Then do the form just like this. psComboBox1

Step 2

Double click the form to fire the code editor.

Step 3

In the code editor, set the import above the Public Class.
  1. Imports MySql.Data.MySqlClient

Step 4

Initialize all the classes that are needed inside the Public Class.
  1. Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=;database=test;sslMode=none")
  2. Dim cmd As MySqlCommand
  3. Dim da As MySqlDataAdapter
  4. Dim dt As DataTable
  5. Dim sql As String

Step 5

Create a sub procedure for retrieving the data in the combobox.
  1. Private Sub fillComboBox(sql As String, feilds As String, combo As ComboBox)
  2. Try
  3. con.Open()
  4. cmd = New MySqlCommand
  5. With cmd
  6. .Connection = con
  7. .CommandText = sql
  8. End With
  9. da = New MySqlDataAdapter
  10. da.SelectCommand = cmd
  11. dt = New DataTable
  12. da.Fill(dt)
  13.  
  14. With combo
  15. .DataSource = dt
  16. .DisplayMember = feilds
  17. End With
  18.  
  19. Catch ex As Exception
  20. MsgBox(ex.Message)
  21. Finally
  22. con.Close()
  23. da.Dispose()
  24. End Try
  25. End Sub

Step 6

Do this following code for retrieving data on the first load of the form.
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. fillComboBox("select * from tblrecords", "Subject", cboSubject)
  3.  
  4. fillComboBox("select * from tblrecords", "Course", cboCourse)
  5.  
  6. fillComboBox("select * from tblrecords", "Category", cboCategory)
  7. End Sub

Step 7

Go to the design views and double click the “Clear All” button to fire the click event handler of it. After that, create a code for clearing the entire combo box with data source.
  1. Try
  2.  
  3. For Each combo In Me.Controls
  4. If TypeOf combo Is ComboBox Then
  5. combo.DataSource = Nothing
  6. End If
  7. Next combo
  8.  
  9. Catch ex As Exception
  10. MsgBox(ex.Message)
  11. End Try
Note: add mysql.data.dll for the references to access MySQL Library The complete sourcecode is included. You can download it and run it on your computer. For more question about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT FB Account – https://www.facebook.com/onnaj.soicalap

Add new comment