Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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?
0
AmyFPA
Asked:
AmyFPA
  • 7
  • 4
  • 2
  • +1
1 Solution
 
scottlafoyCommented:
it would help trouble shoot if we could see the code in question
0
 
AmyFPAAuthor Commented:
Scott - There isn't really code for this.  It's done with properties of the ComboBox.
0
Technology Partners: 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!

 
AmyFPAAuthor Commented:
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
 
Anil GolamariCommented:
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
 
AmyFPAAuthor Commented:
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
 
CodeCruiserCommented:
Which property of the combobox did you bind to the database? SelectedValue?
0
 
AmyFPAAuthor Commented:
CodeCruiser -
The DataSource is the BindingSource that the ComboBox made.
The DisplayMember is the item description.
The ValueMember is the item code.
0
 
CodeCruiserCommented:
And which property is bound to the datasource which saves it to DB (the one which saves wrong value).
0
 
AmyFPAAuthor Commented:
DisplayMember is set to the item description.  This is what gets saved to the other table.
0
 
CodeCruiserCommented:
>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
 
AmyFPAAuthor Commented:
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
 
AmyFPAAuthor Commented:
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
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now