本文介绍了asp c中给定的输入字符串格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i'm trying to read the text box fields values and checking if they are null assigning the zero integer value. then i'm assigning the everytextbox.text to integer values. but here it is showing error as Input string was not in a correct format. how to overcome this <pre lang="c#">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)
推荐答案
试试这个 使用 TryParse 而不是 Parse Try this use TryParse instead of Parse int yearofpurchase=0, monthofpurchase=0, CurrentMonth=0, CurrentYearend=0; float Cost=0, DepnRate1=0; int.TryParse(txtYearofPurchased.Text, out yearofpurchase); int.TryParse(txtYearofPurchased.Text, out yearofpurchase); int.TryParse(txtMonthofPurchased.Text, out monthofpurchase); int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);// present month int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);// present year int.TryParse(txtCost.Text, out Cost); int.TryParse(txtDepnRate1.Text, out DepnRate1);
你似乎在最后两行中有一个拼写错误。尝试使用以下内容替换它们: You seem to have a typo in the last two lines. Try replacing them with the following: float Cost = float.Parse(txtCost.Text); float DepnRate1 = float.Parse(txtDepnRate1.Text)
如果对包含浮点数的字符串使用 Int32.Parse()(是,它们有一个小数点),你会得到一个参数例外。
If you use Int32.Parse() on strings which contain floating point numbers (that is, they have a decimal point), you will get an argument exception.
更多推荐
asp.net c中给定的输入字符串格式不正确
发布评论