Solved

Bound ComboBox grabbing wrong value

Posted on 2011-02-10
14
375 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:AmyFPA
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 5

Expert Comment

by:scottlafoy
ID: 34865138
it would help trouble shoot if we could see the code in question
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 34865333
0
 

Author Comment

by:AmyFPA
ID: 34865719
Scott - There isn't really code for this.  It's done with properties of the ComboBox.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:AmyFPA
ID: 34865811
Lucky - In the article you pointed out, the author makes a query with the ComboBox's property SelectedValue, whereas I've bound my data.  
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 34865983
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.
0
 

Author Comment

by:AmyFPA
ID: 34866095
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."
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34866722
Which property of the combobox did you bind to the database? SelectedValue?
0
 

Author Comment

by:AmyFPA
ID: 34872841
CodeCruiser -
The DataSource is the BindingSource that the ComboBox made.
The DisplayMember is the item description.
The ValueMember is the item code.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34872874
And which property is bound to the datasource which saves it to DB (the one which saves wrong value).
0
 

Author Comment

by:AmyFPA
ID: 34873593
DisplayMember is set to the item description.  This is what gets saved to the other table.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 125 total points
ID: 34883568
>This is what gets saved to the other table.
For it to be save, a property has to be bound to another binding source etc. Make sure this property is SelectedValue not Text.
0
 

Author Comment

by:AmyFPA
ID: 34888045
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.
0
 

Author Closing Comment

by:AmyFPA
ID: 34888084
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!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34895391
Glad to help :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question