Link to home
Create AccountLog in
Avatar of Amy
AmyFlag for United States of America

asked on

Bound ComboBox grabbing wrong value

My ComboBox isn't storing the right value to the database.  I'd like to show the user the item description but store the item's code to the database

I have the DisplayMember set to the item description and the ValueMember set to the item's code, but it stores what I have in DisplayMember instead of what I have in ValueMember.  Whichever field is showing in the ComboBox is what is saved to the database.

I've tried switching them around, but it always saves to the database whatever is shown.

I've tried doing it in code instead of by setting the properties and the same thing happens.

Can there be something else that I have set wrong that would cause this?
Avatar of scottlafoy
Flag of Canada image

it would help trouble shoot if we could see the code in question
Avatar of Amy


Scott - There isn't really code for this.  It's done with properties of the ComboBox.
Avatar of Amy


Lucky - In the article you pointed out, the author makes a query with the ComboBox's property SelectedValue, whereas I've bound my data.  
let us assume that your dropdown list  id is dd1.

Now you want to assign the selected value to some x.

then you can  work

x = dd1.text;

Can you try this and see what it is storing for you.
Avatar of Amy


Lucky - I'm not sure I understand you.  Do you mean, if the user types "x" into the ComboBox, what is stored to the database?  I just now did that and I verified that it does store the "x."
Which property of the combobox did you bind to the database? SelectedValue?
Avatar of Amy


CodeCruiser -
The DataSource is the BindingSource that the ComboBox made.
The DisplayMember is the item description.
The ValueMember is the item code.
And which property is bound to the datasource which saves it to DB (the one which saves wrong value).
Avatar of Amy


DisplayMember is set to the item description.  This is what gets saved to the other table.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Amy


Both SelectedValue and Text are set to "(none)".  Does one of them need to be set to something?

I just now tried setting SelectedValue to the item code, but the item description is still what's saved to the database.
Avatar of Amy


That was it.  My error wasn't in the property settings.  It was in the code.  

In the following line of code, I changed .Text to .SelectedValue
objSQL.Parameters.AddWithValue("@csr", cboCSR.SelectedValue)

Thank you!
Glad to help :-)