Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm making a dynamic table with textboxes, and from each row I will summarize the prices based on the choices they have made. Thif I have 10 rows, I have 10 prizes that I would like to summarize in a literal sum. But my counter does not work properly, it only takes the last row in my table and putting in total


What I have tried:

string total = "";
for (int j = 0; j < Rows; j++)
{
    TextBox t = (TextBox)(PlaceHolder1.FindControl("txtPrice_" + j));

    string[] values = {t.Text};
    double number;
    CultureInfo culture = null;

    foreach (string value in values)
    {
        try
        {
            culture = CultureInfo.CreateSpecificCulture("dk-DK");
            number = Double.Parse(value, culture);


            total = value.ToString();
        }
        catch (FormatException)
        {

            culture = CultureInfo.CreateSpecificCulture("dk-DK");


            total = value.ToString();
        }


    }
    litSum.Text = total;
}
Posted
Updated 4-Mar-18 0:21am
Comments
George Swan 4-Mar-18 5:24am    
I'm not sure what you are trying to achieve but to sum values in a loop you need to add the present value to the previous total. You seem to be replacing the total with latest value during each iteration.

1 solution

Well yes.
What you are doing is this:
C#
int total = 0;
for (int i = 1; i < 10; i++)
   {
   total = i;
   }
Console.WriteLine(total);
And you are surprised when the result is always "9".
Of course it is: you overwrite the value of total each time you go round the loop.
What you need to do is:
int total = 0;
for (int i = 1; i < 10; i++)
   {
   total += i;
   }
Console.WriteLine(total);
And you will get the sum.
But you don't need to be so complicated!
culture = CultureInfo.CreateSpecificCulture("dk-DK");
NumberStyles style = NumberStyles.Number;
double number;
double total = 0.0;
foreach (string value in values)
    {
    if (Double.TryParse(value, style, culture, out number))
       {
       total += Number;
       }
    }
litSum.Text = total.ToString();
 
Share this answer
 
v2
Comments
Martin Lauritsen 4-Mar-18 7:12am    
It works thank you so much, i triede to change to decimal, so it keeps the decimals. But it's not working, you have any idea why ?:) i accept the solution
OriginalGriff 4-Mar-18 7:23am    
Without seeing your code and exactly what you did? No idea! :laugh:
Always assume we can't see your screen...
Martin Lauritsen 4-Mar-18 7:36am    
haha ofc ;)
decimal Number = 0;
decimal total = 0;

for (int j = 0; j < Rows; j++)
{
TextBox t = (TextBox)(PlaceHolder1.FindControl("txtPrice_" + j));

string[] values = { t.Text };

CultureInfo culture = null;

culture = CultureInfo.CreateSpecificCulture("dk-DK");
NumberStyles style = NumberStyles.Number;


foreach (string value in values)
{
if (decimal.TryParse(value, style, culture, out Number))
{
total += Number;
}
litSum.Text = String.Format("{0:0.00}", total.ToString());
}
}
OriginalGriff 4-Mar-18 7:58am    
Why are you creating an array of one value, just to process it with a foreach loop? Makes no real sense...

Start by looking at exactly what is in your text boxes - and I mean exactly, use the debugger to find out - and look at what it returns as a result of the TryParse.
Get rid of the array and the foreach - you don't need either - and move the culture and style definitions outside your outer loop.

What inputs do you get, and what happens when you get them?

            decimal number = 0;
            decimal total = 0;
            CultureInfo culture = CultureInfo.CreateSpecificCulture("dk-DK");
            NumberStyles style = NumberStyles.Number;

            for (int j = 0; j < Rows; j++)
                {
                TextBox t = (TextBox)(PlaceHolder1.FindControl("txtPrice_" + j));
                if (t != null)
                    {
                    string value = t.Text;
                    if (decimal.TryParse(value, style, culture, out number))
                        {
                        total += number;
                        }
                    }  // breakpoint this line, and look at value and number
                }
            litSum.Text = String.Format("{0:0.00}", total.ToString());
Martin Lauritsen 4-Mar-18 8:30am    
i get the number out like this 58750 and it should be 587.50

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900