How to Get the ValueMember of a ComboBox in C# and MySQL Database

In this tutorial, I will teach you how to get a valuemember of a combobox using C# and MySQL database. This method has the capability to get ID of the name of person inside a dropdown selection and it would be displayed inside a listbox . This simple manipulation of data will help you when you are dealing with combobox. Lets begin.

Creating Application

Step 1

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

Step 2

Do the form just like shown below. ps2

Step 3

Open the code editor by pressing the F7 on your keyboard. In the code editor, add a namespace to access MySQL libraries
  1.  
  2. using MySql.Data.MySqlClient;

Step 4

Create a connection between C# and MySQL database. After that, declare all the classes and a string variable that is needed.
  1.  
  2.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none");
  3.         MySqlCommand cmd;
  4.         MySqlDataAdapter da;
  5.         DataTable dt;
  6.         String sql;  

Step 5

Create a method to fill the data in a combobox.
  1.  
  2.         private void fill_data(string sql,ComboBox cbo)
  3.         {
  4.             try
  5.             {
  6.                 con.Open();
  7.                 cmd = new MySqlCommand();
  8.                 da = new MySqlDataAdapter();
  9.                 dt = new DataTable();
  10.  
  11.                 cmd.Connection = con;
  12.                 cmd.CommandText = sql;
  13.  
  14.                 da.SelectCommand = cmd;
  15.                 da.Fill(dt);
  16.  
  17.                 cbo.DataSource = dt;
  18.                 cbo.DisplayMember = "fname";
  19.                 cbo.ValueMember ="idno";
  20.                 cbo.Text = "Select";
  21.  
  22.  
  23.  
  24.             }catch(Exception ex)
  25.             {
  26.                 MessageBox.Show(ex.ToString());
  27.             }
  28.             finally
  29.             {
  30.                 con.Close();
  31.                 da.Dispose();
  32.             }
  33.         }
  34.        

Step 6

Write the following codes to clear the listbox and fill the data in a combobox in the first load of the form.
  1.  
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             sql = "SELECT * FROM `tblstudent`";
  5.             fill_data(sql, comboBox1);
  6.             listBox1.Items.Clear();
  7.         }

Step 7

Double click a combobox and add the following codes to add and display a valuemember of a combobox inside the listbox.
  1.    
  2.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  3.         {
  4.             listBox1.Items.Clear();
  5.             listBox1.Items.Add(comboBox1.SelectedValue.ToString());
  6.         }
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