Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MySQL query Help
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 7:55am
Edited 5-Apr-13 11:05am
Nelek59.4K
v2
Comments
Zoltán Zörgő at 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 at 5-Apr-13 13:06pm
   
When i just print the above I get system.windows.control.comboboxitem error
Zoltán Zörgő at 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 at 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ő at 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 at 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ő at 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
good
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. http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[^] 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,
                            CultureInfo.GetCultureInfo("en-US").NumberFormat);
  Permalink  
v2
Comments
Member 9611735 at 5-Apr-13 15:57pm
   
Unfortunately your solution didn't work as it didn't recognise the SelectedItem.Value.ToString() part.
GregWyatt at 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 at 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
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
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