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].Fi ndControl( "txtaddamt ")).Text = ((TextBox)ngvr.Cells[4].Fi ndControl( "txtaddamt ")).Text.R eplace("$" , "").Replace(",", "");
if (decimal.TryParse(((TextBo x)ngvr.Cel ls[4].Find Control("t xtaddamt") ).Text, out toaddnew))
{
addtotal += toaddnew;
((TextBox)ngvr.Cells[4].Fi ndControl( "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?
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].Fi
if (decimal.TryParse(((TextBo
{
addtotal += toaddnew;
((TextBox)ngvr.Cells[4].Fi
}
}
}
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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."
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?
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?
ASKER
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?
ASKER
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?
ASKER
The error comes through a messagebox. yeah I will try setting a breakpoint.
ASKER
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(t xtaddamt.T ext)))
{
if (decimal.TryParse(txtaddam t.Text, NumberStyles.Currency, null, out toaddnew))
{
addtotal += toaddnew;
txtaddamt.Text = toaddnew.ToString();
}
}
}
when I leave a textbox blank, it checks this if(!string.IsNullOrEmpty(t xtaddamt.T ext)))
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.
if (ngvr.RowType == DataControlRowType.DataRow
{
decimal toaddnew = 0;
TextBox txtaddamt = ngvr.Cells[4].FindControl(
if(!string.IsNullOrEmpty(t
{
if (decimal.TryParse(txtaddam
{
addtotal += toaddnew;
txtaddamt.Text = toaddnew.ToString();
}
}
}
when I leave a textbox blank, it checks this if(!string.IsNullOrEmpty(t
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?
ASKER
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(((T extBox)ngv r.Cells[4] .FindContr ol("txtadd amt")).Tex t))
{
if (decimal.TryParse(((TextBo x)ngvr.Cel ls[4].Find Control("t xtaddamt") ).Text, NumberStyles.Currency, null, out toaddnew))
{
addtotal += toaddnew;
((TextBox)ngvr.Cells[4].Fi ndControl( "txtaddamt ")).Text = toaddnew.ToString();
((SA.Web.UI.WebControls.AJ AX.TextBox )ngvr.Cell s[4].FindC ontrol("tx taddamt")) .Update();
}
}
}
}
txtnewtotal.Text = addtotal.ToString();
UpdatePanel4.Update();
decimal addtotal = 0;
foreach (GridViewRow ngvr in gventernewallocation.Rows)
{
if (ngvr.RowType == DataControlRowType.DataRow
{
decimal toaddnew = 0;
if (!string.IsNullOrEmpty(((T
{
if (decimal.TryParse(((TextBo
{
addtotal += toaddnew;
((TextBox)ngvr.Cells[4].Fi
((SA.Web.UI.WebControls.AJ
}
}
}
}
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.
ASKER
I fixed the problem. Here is what I did.
if (((TextBox)gr.Cells[3].Fin dControl(" txtaddamt" )).Text = "")
{
((TextBox)gr.Cells[3].Find Control("t xtaddamt") ).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.
if (((TextBox)gr.Cells[3].Fin
{
((TextBox)gr.Cells[3].Find
}
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.
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].Fin
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();
}