asp.net c# 中给定的输入字符串格式不正确
the given input string was not in a correct format in asp.net c#
我正在尝试读取文本框字段值并检查它们是否为空并分配零整数值。
然后我将 everytextbox.text
分配给整数值。但这里显示错误,因为输入字符串的格式不正确。如何克服这个
if (txtYearofPurchased.Text == null ||
txtMonthofPurchased.Text == null ||
txtCurrentMonthEnd.Text == null ||
txtCurrentYearEnd.Text == null||
txtCost.Text == null ||
txtDepnRate1.Text ==null)
{
txtYearofPurchased.Text = Convert.ToString(0);
txtMonthofPurchased.Text = Convert.ToString(0);
txtCurrentMonthEnd.Text= Convert.ToString(0);
txtCost.Text= Convert.ToString(0);
txtDepnRate1.Text = Convert.ToString(0);
}
int yearofpurchase = Int32.Parse(txtYearofPurchased.Text);
int monthofpurchase = Int32.Parse(txtMonthofPurchased.Text);
int CurrentMonth = Int32.Parse(txtCurrentMonthEnd.Text);// present month
int CurrentYearend = Int32.Parse(txtCurrentYearEnd.Text);// present year
float Cost = Int32.Parse(txtCost.Text);
float DepnRate1 = Int32.Parse(txtDepnRate1.Text) / 100;
float ad = AD();
两件事:
- 您没有使用
if
的 else 块,所以您使用的是 int.Parse
,即使它们已被检测为无效。
- 使用
int.TryParse
而不是 null-check+int.Parse
因为 Text
属性 永远不会为 null,所以它 returns String.Empty
代替。而且你还必须处理无效输入。
像这样:
int yearofpurchase,monthofpurchase,CurrentMonth,CurrentYearend;
float Cost,DepnRate1,ad;
bool validYearOfPurchase = int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
bool validMonthOfPurchase = int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
bool validCurrentMonth = int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);
bool validCurrentYearend= int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);
if(!validYearOfPurchase || !validMonthOfPurchas || !validCurrentMonth || !validCurrentYearend)
{
txtYearofPurchased.Text = "0";
txtMonthofPurchased.Text = "0";
txtCurrentMonthEnd.Text = "0";
txtCurrentYearEnd.Text = "0";
}
else
{
// ...
}
int yearofpurchase;
int monthofpurchase;
int CurrentMonth;
int CurrentYearend;
float Cost;
float DiffinAccumdepnatbeggining;
float DepnRate1;
bool res = int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
bool MOP = int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
bool CM = int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);// present month
bool CYE = int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);// present year
bool cost = float.TryParse(txtCost.Text, out Cost);
bool DCB = float.TryParse(txtDiffinAccumdepnatbeggining.Text, out DiffinAccumdepnatbeggining);
bool DR = float.TryParse(txtDepnRate1.Text, out DepnRate1);
通常TextBox.Text
不能是null
,而是一个空字符串。另外 Convert.ToString(0)
是 "0"
,所以我会像这样更改您的代码:
if (String.IsNullOrWhitespace(txtYearofPurchased.Text) ||
String.IsNullOrWhitespace(txtMonthofPurchased.Text) ||
String.IsNullOrWhitespace(txtCurrentMonthEnd.Text) ||
String.IsNullOrWhitespace(txtCurrentYearEnd.Text) ||
String.IsNullOrWhitespace(txtCost.Text) ||
String.IsNullOrWhitespace(txtDepnRate1.Text))
{
txtYearofPurchased.Text = "0";
txtMonthofPurchased.Text = "0";
txtCurrentMonthEnd.Text= "0";
txtCost.Text= "0";
txtDepnRate1.Text = "0";
}
int yearofpurchase = Int32.Parse(txtYearofPurchased.Text);
int monthofpurchase = Int32.Parse(txtMonthofPurchased.Text);
int CurrentMonth = Int32.Parse(txtCurrentMonthEnd.Text);// present month
int CurrentYearend = Int32.Parse(txtCurrentYearEnd.Text);// present year
float Cost = Int32.Parse(txtCost.Text);
float DepnRate1 = Int32.Parse(txtDepnRate1.Text) / 100;
我正在尝试读取文本框字段值并检查它们是否为空并分配零整数值。
然后我将 everytextbox.text
分配给整数值。但这里显示错误,因为输入字符串的格式不正确。如何克服这个
if (txtYearofPurchased.Text == null ||
txtMonthofPurchased.Text == null ||
txtCurrentMonthEnd.Text == null ||
txtCurrentYearEnd.Text == null||
txtCost.Text == null ||
txtDepnRate1.Text ==null)
{
txtYearofPurchased.Text = Convert.ToString(0);
txtMonthofPurchased.Text = Convert.ToString(0);
txtCurrentMonthEnd.Text= Convert.ToString(0);
txtCost.Text= Convert.ToString(0);
txtDepnRate1.Text = Convert.ToString(0);
}
int yearofpurchase = Int32.Parse(txtYearofPurchased.Text);
int monthofpurchase = Int32.Parse(txtMonthofPurchased.Text);
int CurrentMonth = Int32.Parse(txtCurrentMonthEnd.Text);// present month
int CurrentYearend = Int32.Parse(txtCurrentYearEnd.Text);// present year
float Cost = Int32.Parse(txtCost.Text);
float DepnRate1 = Int32.Parse(txtDepnRate1.Text) / 100;
float ad = AD();
两件事:
- 您没有使用
if
的 else 块,所以您使用的是int.Parse
,即使它们已被检测为无效。 - 使用
int.TryParse
而不是 null-check+int.Parse
因为Text
属性 永远不会为 null,所以它 returnsString.Empty
代替。而且你还必须处理无效输入。
像这样:
int yearofpurchase,monthofpurchase,CurrentMonth,CurrentYearend;
float Cost,DepnRate1,ad;
bool validYearOfPurchase = int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
bool validMonthOfPurchase = int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
bool validCurrentMonth = int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);
bool validCurrentYearend= int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);
if(!validYearOfPurchase || !validMonthOfPurchas || !validCurrentMonth || !validCurrentYearend)
{
txtYearofPurchased.Text = "0";
txtMonthofPurchased.Text = "0";
txtCurrentMonthEnd.Text = "0";
txtCurrentYearEnd.Text = "0";
}
else
{
// ...
}
int yearofpurchase;
int monthofpurchase;
int CurrentMonth;
int CurrentYearend;
float Cost;
float DiffinAccumdepnatbeggining;
float DepnRate1;
bool res = int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
bool MOP = int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
bool CM = int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);// present month
bool CYE = int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);// present year
bool cost = float.TryParse(txtCost.Text, out Cost);
bool DCB = float.TryParse(txtDiffinAccumdepnatbeggining.Text, out DiffinAccumdepnatbeggining);
bool DR = float.TryParse(txtDepnRate1.Text, out DepnRate1);
通常TextBox.Text
不能是null
,而是一个空字符串。另外 Convert.ToString(0)
是 "0"
,所以我会像这样更改您的代码:
if (String.IsNullOrWhitespace(txtYearofPurchased.Text) ||
String.IsNullOrWhitespace(txtMonthofPurchased.Text) ||
String.IsNullOrWhitespace(txtCurrentMonthEnd.Text) ||
String.IsNullOrWhitespace(txtCurrentYearEnd.Text) ||
String.IsNullOrWhitespace(txtCost.Text) ||
String.IsNullOrWhitespace(txtDepnRate1.Text))
{
txtYearofPurchased.Text = "0";
txtMonthofPurchased.Text = "0";
txtCurrentMonthEnd.Text= "0";
txtCost.Text= "0";
txtDepnRate1.Text = "0";
}
int yearofpurchase = Int32.Parse(txtYearofPurchased.Text);
int monthofpurchase = Int32.Parse(txtMonthofPurchased.Text);
int CurrentMonth = Int32.Parse(txtCurrentMonthEnd.Text);// present month
int CurrentYearend = Int32.Parse(txtCurrentYearEnd.Text);// present year
float Cost = Int32.Parse(txtCost.Text);
float DepnRate1 = Int32.Parse(txtDepnRate1.Text) / 100;