Link to home
Start Free TrialLog in
Avatar of siddhuoops
siddhuoops

asked on

problem in running total

Hi all,
        I am calculating a running total in a gridview using the textchanged event. this is my code.

protected void txtaddamt_Changed(object sender, EventArgs e)
    {
       
        decimal addtotal = 0;
        foreach (GridViewRow ngvr in gventernewallocation.Rows)
        {
            if (ngvr.RowType == DataControlRowType.DataRow)
            {
                decimal toaddnew = 0;
                ((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text = ((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text.Replace("$", "").Replace(",", "");
                if (decimal.TryParse(((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text, out toaddnew))
                {
                    addtotal += toaddnew;
                    ((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text = toaddnew.ToString();
                }
            }
        }
        txtnewtotal.Text = addtotal.ToString();
    }

I get the running total which is fine. But when I leave the textbox blank without any numbers, I get the error "Input string was not in a correct format." By default, I have 0 to the textbox. How can I fix this?
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Heres a slightly tidier version of your code with teh fix to prevent it trying to add blanks:

protected void txtaddamt_Changed(object sender, EventArgs e)
    {
       
        decimal addtotal = 0;
        foreach (GridViewRow ngvr in gventernewallocation.Rows)
        {
            if (ngvr.RowType == DataControlRowType.DataRow)
            {
                decimal toaddnew = 0;
                TextBox t = (TextBox)ngvr.Cells[4].FindControl("txtaddamt");

                t.Text = t.Text.Replace("$", "").Replace(",", "");
                if (t.Text.Length > 0)
                {
                    if (decimal.TryParse(t.Text, out toaddnew))
                   {
                        addtotal += toaddnew;
                        t.Text = toaddnew.ToString();
                   }
                }
            }
        }
        txtnewtotal.Text = addtotal.ToString();
    }
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of siddhuoops
siddhuoops

ASKER

but the problem is when the user enter a number in the textbox and later decides not to use it and deletes it and leave the textbox blank, then they can't go furthur because they get the error message "Input string was not in a correct format".
dstanley9, when I use your code, I get the error message,
"No overload for method 'TryParse' takes '4' arguments."
Odd - Here's the method signiature from MSDN:
public static bool TryParse (
      string s,be requiring a numeric
      NumberStyles style,
      IFormatProvider provider,
      out decimal result
)

Do you have any validators in the gridview that may be requiring a numeric value?  Or any other code that's trying to parse the value?
Nope..first of all I don't get the NumberStyles.Currency. Am I missing any namespace?
System.Globalization
But now I'm curious because if you're using TryParse you shouldn't be getting an exception.  Are you getting an unhandled exception or an error message on the client?
I am not getting any error messages but when I delete sth from the textbox and leave it blank, I get the same error: "Input string was not in a correct format." I think it didn't like the blank. I am checking this because we have some dealers that won't put anything or just leave the textbox blank.
But how do you see the error?  Messagebox?  Exception dialog?  Can you put a breakpoint  in the code and see where the error is occuring?
The error comes through a messagebox. yeah I will try setting a breakpoint.
I set a break point and in the code below,
 if (ngvr.RowType == DataControlRowType.DataRow)
            {
                decimal toaddnew = 0;
                TextBox txtaddamt =  ngvr.Cells[4].FindControl("txtaddamt") as TextBox;
                if(!string.IsNullOrEmpty(txtaddamt.Text)))
                {
                  if (decimal.TryParse(txtaddamt.Text, NumberStyles.Currency, null, out toaddnew))
                  {
                      addtotal += toaddnew;
                      txtaddamt.Text = toaddnew.ToString();
                  }
                }
            }
when I leave a textbox blank, it checks this  if(!string.IsNullOrEmpty(txtaddamt.Text)))
and it jumps off the code since the textbox is blank. then I get the message box saying Input string was not in a correct format.
Then the problem isn't with that code.  Are you using a standard text box or some 3rd party control?  Do you have any validators that could be popping up the message box.  Are you calling alert() anywhere in the client-side javascript?  Are you trying to parse that value anywhere else?
I am using a third party control...the gridview is inside an update panel and the textbox is not a standard textbox. here is my full code, I thought using a 3rd party control don't have to do anything with this.

decimal addtotal = 0;
        foreach (GridViewRow ngvr in gventernewallocation.Rows)
        {
            if (ngvr.RowType == DataControlRowType.DataRow)
            {
                decimal toaddnew = 0;
                if (!string.IsNullOrEmpty(((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text))
                {
                    if (decimal.TryParse(((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text, NumberStyles.Currency, null, out toaddnew))
                    {
                        addtotal += toaddnew;
                        ((TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Text = toaddnew.ToString();
                        ((SA.Web.UI.WebControls.AJAX.TextBox)ngvr.Cells[4].FindControl("txtaddamt")).Update();
                    }
                }
            }
        }
        txtnewtotal.Text = addtotal.ToString();
        UpdatePanel4.Update();
If the third-party control is some sort of numeric input control I'd consider that as the source.  Look at the properties for some sort of property that allows blank values.  Since the error is shoiwinf up in an alert window if you aren't explicitly creating it in JavaScript I think your textbox control may be the culprit.
I fixed the problem. Here is what I did.

if (((TextBox)gr.Cells[3].FindControl("txtaddamt")).Text = "")
{
     ((TextBox)gr.Cells[3].FindControl("txtaddamt")).Text  = "0";
}

since I am using the 3rd control, I am just updating that control. This if statement is in foreach loop, so it checks for each textbox.

Thank You for giving me some great ideas on all this.