• We just launched and are currently in beta. Join us as we build and grow the community.

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

Juanita

SMM Specialist
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
50
Likes
58
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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#.
creating_application_c_3.png

Step 2

Do the form just like shown below.
ps_get1.png

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. 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. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=dbsubjects;sslMode=none"

    )

    ;
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. String

    sql;

Step 5

Create a method to fill the data in a combobox.

  1. private

    void

    fill_data(

    string

    sql,ComboBox cbo)
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;
  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. cmd.

    Connection

    =

    con;
  10. cmd.

    CommandText

    =

    sql;

  11. da.

    SelectCommand

    =

    cmd;
  12. da.

    Fill

    (

    dt)

    ;

  13. cbo.

    DataSource

    =

    dt;
  14. cbo.

    DisplayMember

    =

    "fname"

    ;
  15. cbo.

    ValueMember

    =

    "idno"

    ;
  16. cbo.

    Text

    =

    "Select"

    ;



  17. }

    catch

    (

    Exception ex)
  18. {
  19. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  20. }
  21. finally
  22. {
  23. con.

    Close

    (

    )

    ;
  24. da.

    Dispose

    (

    )

    ;
  25. }
  26. }


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. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. sql =

    "SELECT * FROM `tblstudent`"

    ;
  4. fill_data(

    sql, comboBox1)

    ;
  5. listBox1.

    Items

    .

    Clear

    (

    )

    ;
  6. }

Step 7

Double click a combobox and add the following codes to add and display a valuemember of a combobox inside the listbox.

  1. private

    void

    comboBox1_SelectedIndexChanged(

    object

    sender, EventArgs e)
  2. {
  3. listBox1.

    Items

    .

    Clear

    (

    )

    ;
  4. listBox1.

    Items

    .

    Add

    (

    comboBox1.

    SelectedValue

    .

    ToString

    (

    )

    )

    ;
  5. }

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top