1

How can I save the correct ValueMember of a ComboBox to the entities/db? My Winform has ComboBoxes that take Values from Entities of a Lookup Data tabel, as in the following code:

    private void FillComboBoxes()
    {
        chargedToComboBox.Invalidate();
        ModelCS ctx = new ModelCS();
        var query1 = from a in ctx.LuDatas
                     where a.Category == "Charged To" && a.IsActive == true
                     select new { LuValueMember = a.LuValueMember,
                                  LuDisplayMember = a.LuDisplayMember };

        var chargedTo = query1.ToList();
        chargedToComboBox.DataSource = chargedTo;
        chargedToComboBox.DisplayMember = "LuDisplayMember";   
        chargedToComboBox.ValueMember = "LuValueMember";     

        string ch = chargedToComboBox.SelectedValue.ToString();
        MessageBox.Show(ch);      // variable ch shows the CORRECT
                                  // ValueMember
        chargedToTextBox.Text = ch;     // variable ch show the UNDESIRABLE                 
                                        // DisplayMember
        this.Refresh();

    }

On SaveChanges() I get the following error: 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll

To find out what is happening, I added a TextBox to the form, and attempted to store in the what I thought was the ComboBox.ValueMember. To do this, I stored the the ComboBox.SelectedValue in a variable (ch) and and stored this variable in the TextBox.Text. What shows up in the TextBox.Text is the DisplayMember of the Combo, not the ValueMember.

To test why, I added a MessageBox to the code to see the vaslue of 'ch'; it shows the corresct value of the ValueMember.

How can it be that in the MessageBox 'ch' has one value and in the TextBox.Text it has another?

All I wanted was to extract a lookup list from the LuData entities, show the choices by name in the ComboBox and store the value by a code in the database.

HES
  • 21
  • 5
  • Affraid your form have some "side effect", maybe your `chargedToTextBox` have some event handlers which update value after you have set it to `ch`. And you don't need to use `this.Refresh` which just redraw whole form. You only changed values of already existed controls, so no needs to redraw a form – Fabio Oct 15 '16 at 16:48
  • Thanks Fabio for the this.Refresh(). Took it out. Also used the line below from Shadow. No change. Will research further. – HES Oct 15 '16 at 18:44
  • Can you check your whole form for occurrences of `chargedToTextBox`? maybe you are doing some databinding with it? You can subscribe to its `TextChanged`-event and set a breakpoint in the event handler to see in the stacktrace who is changing the text in which situation. – Kai Thoma Oct 18 '16 at 14:33

1 Answers1

1

Try this:

chargedToTextBox.Text = chargedToComboBox.SelectedItem.ToString();
shadow
  • 1,883
  • 1
  • 16
  • 24
  • Thanks for your swift response. Sorry did not work. The "chargedToComboBox.SelectedItem.ToString()" still holds the DisplayMember and not the ValueMember that is needed. – HES Oct 15 '16 at 18:40