Convert.ToInt32()的神秘FormatException ... [英] Mysterious FormatException for Convert.ToInt32()...

查看:58
本文介绍了Convert.ToInt32()的神秘FormatException ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为代码项目上的所有专家欢呼!由Convert.ToInt32引发,它使用来自ASP .NET 2.0格式的输入值,且带有*验证:

<br />ERROR 16 Jul 2009 09:01:13,970 (Webpage code-behind class name) - Failed conversion of value<br />System.FormatException: Input string was not in a correct format.<br />   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)<br />   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)<br />   at (name of calling function in ASP.NET code-behind)<br />



Web网页的代码如下:<并在网页上对txbWeight TextBox控件进行以下验证:

<br /><asp:CompareValidator runat="server" ControlToValidate="txbWeight" Type="Integer" <br />Operator="GreaterThanEqual" ValueToCompare="0" Text="Positive number only for weight."<br />Display="Dynamic" /><br />



我不明白为什么抛出此异常是因为验证器确保从用户处接收到整数(txbWeight还具有RequiredFieldValidator).奇怪的是,尽管抛出了异常,但dataObject仍接收到该值!我不认为我需要调用Trim(),并且过去尝试使用此方法也无法解决问题.

有什么想法吗?

解决方案

<总是使用int.tryparse,而不是convert.toint32.您是否在其中输入文字,是否需要告诉验证程序接受一个空字段?


结果表明,对于我的Web应用程序中的某个导航路径(即重定向),我禁用了验证控件,并且这些字段可能具有空值,导致此例外).从那以后,我重写了这个错误的代码,确实改成了克里斯蒂安建议的更好的TryParse(...)方法.


Cheers to all the mavens out there on codeproject!

I''ve banged my head over this one for some time, and I don''t understand the reason for this FormatException (log entry) as thrown by Convert.ToInt32 that uses an input value from an ASP .NET 2.0 form *with* validation:

<br />ERROR 16 Jul 2009 09:01:13,970 (Webpage code-behind class name) - Failed conversion of value<br />System.FormatException: Input string was not in a correct format.<br />   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)<br />   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)<br />   at (name of calling function in ASP.NET code-behind)<br />



The code-behind of the web page is as follows:

<br />try<br />{<br />    dataObject.Weight = Convert.ToInt32(txbWeight.Text);<br />}<br />catch (Exception e)<br />{<br />    log.Error("Failed conversion of value", e);<br />}<br />



And the web page has the following validation for the txbWeight TextBox control:

<br /><asp:CompareValidator runat="server" ControlToValidate="txbWeight" Type="Integer" <br />Operator="GreaterThanEqual" ValueToCompare="0" Text="Positive number only for weight."<br />Display="Dynamic" /><br />



I don''t understand why this exception is thrown, because the validator ensures that an integer is being received from the user (txbWeight also has a RequiredFieldValidator). The odd thing is that dataObject receives the value despite the exception being thrown!? I don''t believe I need to call Trim(), and trying this in the past did not fix the issue.

Any ideas?

解决方案

Always use int.tryparse, not convert.toint32. Do you have text in there, do you need to tell the validator to accept an empty field ?


Turns out that for a certain navigation path (i.e. a redirection) in my web application, I was disabling the validation controls and the fields could have empty values resulting in this exception). I have since rewritten this bad code and indeed have changed over to the much better TryParse(...) method as Christian suggested.


这篇关于Convert.ToInt32()的神秘FormatException ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆