Solved

Bound ComboBox grabbing wrong value

Posted on 2011-02-10
14
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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