输入字符串的格式不正确。为什么? [英] Input string was not in a correct format. Why?
本文介绍了输入字符串的格式不正确。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在对Web表单进行一些舍入和计算。当我调试程序时,其他一切都有效,直到我进入Rounding部分。我得到错误输入字符串格式不正确。所以我注释掉那个部分并再次调试程序。舍入工作和显示。当它试图添加数字时,它会抛出输入字符串的格式错误。我做错了什么?
以下是所有代码:
I am doing some Rounding and calculations on a Web form. When I debug the program everything else works until I get to the Rounding part. I get and error "Input string was not in a correct format". So I comment out that section and debugged the program again. The Rounding works and displays. When it is trying to add the numbers it throws the Input string was not in a correct format error. What did I do wrong?
Here is all of the code:
protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));
//int j = 12;
TextBoxTHUGDR.Text = Convert.ToString(i / 12.0);
int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));
TextBoxT1234.Text = Convert.ToString(a + b + c + d);
int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));
TextBoxTCNC.Text = Convert.ToString(g + f);
int o = Convert.ToInt32(TextBoxLYTCNC.Text.Replace(",", ""));
int p = Convert.ToInt32(TextBoxTCNC.Text.Replace(",", ""));
TextBoxFTE40.Text = Convert.ToString(Math.Round((Math.Abs(p - o) * 100.0 / ((o)))));
TextBoxFTE40.Text = Math.Round(Convert.ToDouble(TextBoxFTE40.Text), 2).ToString();
RangeValidatorLYTHUGDR.Validate();
RangeValidatorLYTCNC.Validate();
TextBoxTHUGDR.Text = Math.Round(Convert.ToDouble(TextBoxTHUGDR.Text.ToString()), 2).ToString();
TextBoxTHUGDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUGDR.Text));
TextBoxTHUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUG.Text));
TextBoxT1234.Text = string.Format("{0:0,0}", double.Parse(TextBoxT1234.Text));
TextBoxTCNC.Text = string.Format("{0:0,0}", double.Parse(TextBoxTCNC.Text));
TextBoxTHG.Focus();
}
这是错误抛出的地方:
Here is where the error is throwing:
int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
如何解决此问题?
How can I fix this?
推荐答案
首先关闭,不要使用转换 - 改为使用TryParse,因为它返回成功/失败指示符而不是抛出异常。这允许您向用户报告问题而不是程序失败。
这可能是问题所在:文本框不包含有效整数,即使你删除了所有的逗号。我们无能为力 - 这是一个输入错误,你有责任检测这些错误,让用户在继续之前纠正他的输入。
First off, don't use Convert - use TryParse instead, as it returns a "success / fail" indicator instead of throwing an exception. This allows you to report problems back to your user instead of your program failing.
And that's probably what the problem is: the textbox doesn't contain a valid integer, even after you have removed all the commas. Nothing we can do about that - it's an input error and it's your responsibility to detect those, and get the user to correct his inputs before you continue.
这篇关于输入字符串的格式不正确。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文