How to Fill Data in a ComboBox Using C# and MySQL Database

Submitted by: 
Visitors have accessed this post 438 times.

In this tutorial, I will teach you how to fill data in a combobox using c# and mysql database. This method is so useful when you retrieve all the data in the column of the table in the database, to use it for the selection field in the registration form of the system. Just simply follow the step below to see how it works.

Creating Database

Create a Database named “peopledb”.
After that, execute the following query below for creating and adding the data in the table.

  1. CREATE TABLE `tbluser` (
  2. `UserId` int(11) NOT NULL,
  3. `Fullname` varchar(124) NOT NULL
  5. --
  6. -- Dumping data for table `tbluser`
  7. --
  9. INSERT INTO `tbluser` (`UserId`, `Fullname`) VALUES
  10. (3, 'Janno Palacios'),
  11. (4, 'Craig'),
  12. (7, 'cherry lou velez'),
  13. (8, 'velez lou'),
  14. (9, 'jom');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#.

Step 2

Do the Form just like this.

Step 3

Add for your references.

Step 4

Go to the code editor and add using MySql.Data.MySqlClient; above the namespace to access MySQL library;

Step 5

Initialize the connection between mysql and c#. After that, declare all the classes and variables that are needed.

  1. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=janobe;database=peopledb;sslMode=none");
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. string sql;

Step 6

Create a method for filling the data in the combobox.

  1. private void LoadCombo(string sql,string DisplayMember,string ValueMember)
  2. {
  3. try
  4. {
  5. con.Open();
  6. cmd = new MySqlCommand();
  7. cmd.Connection = con;
  8. cmd.CommandText = sql;
  9. da = new MySqlDataAdapter();
  10. da.SelectCommand = cmd;
  11. dt = new DataTable();
  12. da.Fill(dt);
  14. comboBox2.DataSource = dt;
  15. comboBox2.DisplayMember = DisplayMember;
  16. comboBox2.ValueMember = ValueMember;
  20. }
  21. catch(Exception ex)
  22. {
  23. MessageBox.Show(ex.Message);
  25. }finally
  26. {
  27. con.Close();
  29. }
  30. }

Step 7

Go back to the design view, double click the button to fire the click event handler of it and call the method that you have created for filling the data into the combobox.

  1. sql = "SELECT * FROM `tbluser`";
  2. LoadCombo(sql, "Fullname", "UserId");

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.