Click here to Skip to main content
13,147,037 members (61,530 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone. I am trying to work out how to calculate a order by multiplying the values in a label and a combobox. This is what I've got so far

decimal price= Convert.ToDecimal(label1.Content);
          decimal quantity = Decimal.Parse(comboBox3.SelectedItem.ToString());
          decimal total = quantity * price;
          label2.Content = total;

ERROR: Input string was not in a correct format.

Can anybody give me any advice on this matter or give me a better way of doing this?
Posted 5-Apr-13 6:55am
Updated 5-Apr-13 10:05am
Zoltán Zörgő 5-Apr-13 12:59pm
And do you get if you print comboBox3.SelectedItem.ToString(). Be aware of the culture, you are working in: especially decimal and thousand separators are interesting in this context.
Member 9611735 5-Apr-13 13:06pm
When i just print the above I get error
Zoltán Zörgő 5-Apr-13 15:17pm
Do you have any item selected at all?
Try: comboBox3.Items[comboBox3.SelectedIndex].ToString()
Debug! Where exactly do you get the exception. What is in label1.Content?
Member 9611735 5-Apr-13 15:44pm
Still reports the error Input string was not in a correct format. In label1 is the price of the product, in the combobox is the available quantities.
Zoltán Zörgő 5-Apr-13 15:47pm
I was asking about the exact string values! Not the meaning of them. And what is the current culture?
Do you want me to help you or not? If so, please try to answer. Don't forget, I can't see your screen!
Member 9611735 5-Apr-13 15:54pm
the string value stored in label1 is 9,00. Do you think I am carrying out the calculation in the right way i.e should I be changing the values to decimals?
Zoltán Zörgő 5-Apr-13 16:07pm
Again: what is your current culture? 9,00 is not valid in US culture for example!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I believe it is most likely erroring on comboBox3.SelectedItem.ToString(). Typically you have to call a selected item's Value or Text depending on what you want to parse before calling the .ToString function.

So it should look like comboBox3.SelectedItem.Value.ToString()

**FYI I don't know what type of control is being used for your combo box whether that is just a name you chose or if it is a combo box so the call may require a different syntax then what I've stated above depending on the control used. If this doesn't help you, please let me know and I can see what I can do.

I'm going to assume that you are able to access the value correctly. If you can't please let me know.

From your comments above I saw that you are trying to parse the string 9,000 which by itself will cause an error. As the above commenter is eluding to culture info specifys what is partically viewed as acceptable input.

You can also specify number styles as well. A pipe delimiter | placed between NumberStyles means that both will be used.[^] has a list of all the various number styles that can be accepted as valid input.

The below code will process the string 9,000

using System.Globalization;
decimal quantity = decimal.Parse("9,000", NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
Member 9611735 5-Apr-13 15:57pm
Unfortunately your solution didn't work as it didn't recognise the SelectedItem.Value.ToString() part.
GregWyatt 5-Apr-13 16:01pm
Can you please post your aspx code as well so that I may see what types of controls you are using?
Member 9611735 5-Apr-13 16:07pm
XAML code:
combobox grid.column="2" grid.row="2" height="23" horizontalalignment="Left" margin="0,17,0,0" name="comboBox3" verticalalignment="Top" width="120" selectionchanged="comboBox3_SelectionChanged"
comboboxitem name="five">5
comboboxitem name="ten">10
comboboxitem name="fifteen">15
comboboxitem name="twenty">20
comboboxitem name="twentyfive">25
comboboxitem name="thirty">30

Label Grid.Column="5" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="0,24,0,0" Name="label1" VerticalAlignment="Top" Width="117" Grid.ColumnSpan="2"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100