locked
Calculating multiple Lables RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have multiple labels containing DB where the data type is a money value. I would like to calculate the multiple labels and display the result into another label. i have tried using parse as shown in my code below but am getting the error message: "Input string was not in a correct format." 

    Code:

    string resultMC = MealCostLabel.ToString();
    string resultHC = HotelCostLabel.ToString();
    string resultPRC = ParkingRoadTollCostLabel.ToString();
    string resultOC = OtherCostLabel.ToString();
    decimal MC = Decimal.Parse(resultMC);
    decimal HC = Decimal.Parse(resultHC);
    decimal PRC = Decimal.Parse(resultPRC);
    decimal OC = Decimal.Parse(resultOC);
    TotalCostLabel.Text = (MC + HC + PRC + OC).ToString("C");

    Any help would be greatly appreciated.

    Many thanks

    Jonny

    Thursday, September 7, 2017 12:37 PM

Answers

  • User2103319870 posted

    When running no errors occur but the Out MC value shows 0 but the MealCostLabel displays the DB value.

    Most likely the value which you are converting is not a valid decimal value like 45.6,67.8, etc. Also Just a guess are you having currency symbols the use the below code to convert

    decimal MC = 0;
                string resultOC = "$12.45";
    
                decimal.TryParse(resultOC, System.Globalization.NumberStyles.Currency, null, out MC);

    Also you can pass the culture code instead of null 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 7, 2017 3:11 PM

All replies

  • User-359936451 posted

    money values are data type 'double' or 'currency' depending on your database platform.

    Try hanging like this, just as a quick test....

    decimal resultMC = MealCostLabel.text;
    decimal resultHC = HotelCostLabel.text;

    decimal TotalCostLabel = (resultMC+ resultHC);

    string output = CType(TotalCostLabel, string) ;      '  You likely need to cast this to a string value.  

    Basically the point is you need to read the values as doubles or currency, do your math, then cast back to string for display.

    Thursday, September 7, 2017 12:57 PM
  • User-1509636757 posted

    "Input string was not in a correct format." 

    The error occurs since some of string value(s) not able to be parsed into Decimal. I recommend to make use of Decimal.TryParse Method (String, Decimal) (System) It not only converts the string representation of a number to its Decimal equivalent but also returns value indicates whether the conversion succeeded or failed. So, in case of failure happens like above, the code won't break but will move further.

    Here is an example:

    decimal MC;
    Decimal.TryParse(Convert.ToString(MealCostLabel), out MC);
    
    decimal HC;
    Decimal.TryParse(Convert.ToString(HotelCostLabel), out HC);
    
    decimal PRC;
    Decimal.TryParse(Convert.ToString(ParkingRoadTollCostLabel), out PRC);
    
    decimal OC;
    Decimal.TryParse(Convert.ToString(OtherCostLabel), out OC);
    
    TotalCostLabel.Text = (MC + HC + PRC + OC).ToString("C");

    Thursday, September 7, 2017 1:04 PM
  • User-1901014284 posted

    Thank you both for your advice, it is greatly appreciated, when I have run the code above the code runs and no error messages are received but the Total Cost text box displays 0. After breaking down the code to troubleshoot it does not seem as though the parse has been successful as the Decimal out contains a value on 0.

    Thursday, September 7, 2017 1:23 PM
  • User2103319870 posted

    If "MealCostLabel" are label control then you need to use Text property to fetch the value available on labels

    Try with below code

    string resultMC = MealCostLabel.Text.ToString();
                string resultHC = HotelCostLabel.Text.ToString();
                string resultPRC = ParkingRoadTollCostLabel.Text.ToString();
                string resultOC = OtherCostLabel.Text.ToString();
                decimal MC = Decimal.Parse(resultMC);
                decimal HC = Decimal.Parse(resultHC);
                decimal PRC = Decimal.Parse(resultPRC);
                decimal OC = Decimal.Parse(resultOC);
                TotalCostLabel.Text = (MC + HC + PRC + OC).ToString("C");

    Thursday, September 7, 2017 1:30 PM
  • User-1509636757 posted

    Thank you both for your advice, it is greatly appreciated, when I have run the code above the code runs and no error messages are received but the Total Cost text box displays 0. After breaking down the code to troubleshoot it does not seem as though the parse has been successful as the Decimal out contains a value on 0.

    Like A2H said, if MealCostLabel is a label then you will require to use Text property of MealCostLabel and for the rest of the Labels as:

    decimal MC;
    Decimal.TryParse(Convert.ToString(MealCostLabel.Text), out MC);
    Thursday, September 7, 2017 1:35 PM
  • User-1901014284 posted

    Hi a2h, I have tried this and get the same error message as when I use .ToString().

    Thursday, September 7, 2017 2:47 PM
  • User-1901014284 posted

    Hi Kaushalparik27, I have also tried using the .Text as you have demonstrated above. When running no errors occur but the Out MC value shows 0 but the MealCostLabel displays the DB value.

    Thursday, September 7, 2017 2:52 PM
  • User2103319870 posted

    but the MealCostLabel displays the DB value.

    Please provide the value which you are getting from database

    Thursday, September 7, 2017 3:01 PM
  • User-359936451 posted

    I just tested this in a button click event and it works perfectly....

    decimal lbl3 = Label3.Text;
    decimal lbl5 = Label5.Text;
    
    Label6.Text = lbl3 + lbl5;

    label3.text = 12.2

    label5.text = 5.3

    and the value returned that is populated in label6.text correctly displays as 17.5

    so change your code to this....

    decimal resultMC = MealCostLabel.Text;
    decimal resultHC = HotelCostLabel.Text;
    decimal resultPRC = ParkingRoadTollCostLabel.Text;
    decimal resultOC = OtherCostLabel.Text;
    TotalCostLabel.Text = (resultMC + resultHC + resultPRC + resultOC);

    these must all be label controls,

    MealCostLabel

    HotelCostLabel

    ParkingRoadTollCostLabel

    OtherCostLabel

    TotalCostLabel

    Thursday, September 7, 2017 3:03 PM
  • User2103319870 posted

    When running no errors occur but the Out MC value shows 0 but the MealCostLabel displays the DB value.

    Most likely the value which you are converting is not a valid decimal value like 45.6,67.8, etc. Also Just a guess are you having currency symbols the use the below code to convert

    decimal MC = 0;
                string resultOC = "$12.45";
    
                decimal.TryParse(resultOC, System.Globalization.NumberStyles.Currency, null, out MC);

    Also you can pass the culture code instead of null 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 7, 2017 3:11 PM
  • User-1901014284 posted

    Hi march11,

    Thank you, i have tried but i get errors on the code

    "cannot implicitly convert type string to decimal" on the text boxes and the local variable.

    Thursday, September 7, 2017 3:16 PM
  • User-1901014284 posted

    That has worked thank you a2h, greatly appreciate all your help.

    Thursday, September 7, 2017 3:39 PM
  • User-359936451 posted

    Yea you can't use symbols. That is why I showed you the exact values I was using. Had you provided that detail in your original post, we may have gotten to the solution sooner. Live and learn!!!

    Thursday, September 7, 2017 3:51 PM