asp.net c中给定的输入字符串格式不正确

编程入门 行业动态 更新时间:2024-10-26 00:25:32
本文介绍了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中给定的输入字符串格式不正确

本文发布于:2023-11-12 09:40:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不正确   格式   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!