Last post May 27, 2014 11:34 AM by Arslan14
Member
10 Points
63 Posts
May 24, 2014 02:48 AM|gaddamraju4u|LINK
int total=0; int total2; if (lblCounter.Text == null) { lblCounter2.Text = total.ToString(); } else { total2 = Int32.Parse(lblCounter2.Text); total2++; lblCounter2.Text = total2.ToString(); }
Exception Details: System.FormatException: Input string was not in a correct format.
Line 26: else Line 27: { Line 28: total2 = Int32.Parse(lblCounter2.Text); Line 29: total2++; Line 30: lblCounter2.Text = total2.ToString();
All-Star
52503 Points
15665 Posts
May 24, 2014 03:05 AM|oned_gk|LINK
Try this
int total=0; int total2; if (lblCounter.Text.Trim() == string.Empty) { lblCounter2.Text = total.ToString(); } else { total2 = Int32.Parse(lblCounter2.Text); total2++; lblCounter2.Text = total2.ToString(); }
35159 Points
9075 Posts
May 24, 2014 03:07 AM|smirnov|LINK
The error says it all - the value of lblCounter2.Text is not int32.
To avoid an error you can do like this
bool ok = int.TryParse(lblCounter2.Text, out total2); if (!ok) { // String is not a number. }
94 Points
171 Posts
May 27, 2014 06:44 AM|mindominiscient|LINK
I think to tackle this problem in a best manner is to use JavaScript validations and also Server side validations.
None
0 Points
1 Post
May 27, 2014 07:38 AM|donijose|LINK
This happens because the value of lblCounter2.Text can be empty string. Try this
int total=0; int total2; if (lblCounter.Text == null) { lblCounter2.Text = total.ToString(); } else { total2 = !String.IsNullOrWhiteSpace(lblCounter2.Text) ? Int32.Parse(lblCounter2.Text) : 0; total2++; lblCounter2.Text = total2.ToString(); }
May 27, 2014 09:40 AM|smirnov|LINK
donijose lblCounter2.Text can be empty string. Try this ... if (lblCounter.Text == null)
lblCounter2.Text can be empty string. Try this
... if (lblCounter.Text == null)
FYI,
1) an empty string is not the same as null 2) lblCounter.Text should never be null unless the control was added dynamically
159 Points
120 Posts
May 27, 2014 11:34 AM|Arslan14|LINK
gaddamraju4u int total=0; int total2; if (lblCounter.Text == null) { lblCounter2.Text = total.ToString(); } else { total2 = Int32.Parse(lblCounter2.Text); total2++; lblCounter2.Text = total2.ToString(); }
i think you should use
total2 = Convert.ToInt32(lblCounter2.Text);
Member
10 Points
63 Posts
I got error in c# code can any one solve this please ?
May 24, 2014 02:48 AM|gaddamraju4u|LINK
int total=0; int total2; if (lblCounter.Text == null) { lblCounter2.Text = total.ToString(); } else { total2 = Int32.Parse(lblCounter2.Text); total2++; lblCounter2.Text = total2.ToString(); }
Exception Details: System.FormatException: Input string was not in a correct format.
Line 26: else Line 27: { Line 28: total2 = Int32.Parse(lblCounter2.Text); Line 29: total2++; Line 30: lblCounter2.Text = total2.ToString();
All-Star
52503 Points
15665 Posts
Re: I got error in c# code can any one solve this please ?
May 24, 2014 03:05 AM|oned_gk|LINK
Try this
Suwandi - Non Graduate Programmer
All-Star
35159 Points
9075 Posts
Re: I got error in c# code can any one solve this please ?
May 24, 2014 03:07 AM|smirnov|LINK
The error says it all - the value of lblCounter2.Text is not int32.
To avoid an error you can do like this
bool ok = int.TryParse(lblCounter2.Text, out total2);
if (!ok)
{
// String is not a number.
}
Member
94 Points
171 Posts
Re: I got error in c# code can any one solve this please ?
May 27, 2014 06:44 AM|mindominiscient|LINK
I think to tackle this problem in a best manner is to use JavaScript validations and also Server side validations.
None
0 Points
1 Post
Re: I got error in c# code can any one solve this please ?
May 27, 2014 07:38 AM|donijose|LINK
This happens because the value of lblCounter2.Text can be empty string. Try this
All-Star
35159 Points
9075 Posts
Re: I got error in c# code can any one solve this please ?
May 27, 2014 09:40 AM|smirnov|LINK
FYI,
1) an empty string is not the same as null
2) lblCounter.Text should never be null unless the control was added dynamically
Member
159 Points
120 Posts
Re: I got error in c# code can any one solve this please ?
May 27, 2014 11:34 AM|Arslan14|LINK
i think you should use