What combobox property must be filled to make a choosen option reflect a correspondent code?

Hi Experts!

What combobox property must be filled to make a choosen option reflect  a correspondent code?


BD table:

The code I used:

 using (var connection = new MySqlConnection(connectionString))
                var query = "SELECT SB_CR, DESCRICAO FROM cr WHERE TIPO=1";
                using (var command = new MySqlCommand(query, connection))
                    using (var reader = command.ExecuteReader())
                        //Iterate through the rows and add it to the combobox's items
                        while (reader.Read())

                           // It doesn't work
                           //comboBox1.ValueMember = reader.GetString("SB_CR");


Open in new window

So If I choose CASA a correspondent property containing the PK  101 must be available to be used by the program. What's the property ?

Thanks in advance!
Eduardo FuerteAsked:
Who is Participating?
Shaun KlineConnect With a Mentor Lead Software EngineerCommented:
ValueMember and DisplayMember are used for databinding your combo box to a datasource, such as a DataSet.

If you want to use a SQLDataReader, you will need to create a ComboBoxItem, set its Content and Tag properties and then add it to your ComboBox.
Eduardo FuerteAuthor Commented:

Really much easier using Dataset than code as I did.
I had only to turn off constraints in dataset for my particular purpose.
and simply use

  var teste1 = this.comboBox1.SelectedValue;
Eduardo FuerteAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.