输入字符串的格式不正确? [英] Input string was not in a correct format?

查看:93
本文介绍了输入字符串的格式不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到的输入字符串格式问题不正确。



我正在使用gridview

我在这里得到错误

Hi,
I am getting Input string was not in a correct format issues.

I'm using gridview
Im getting error over here

DataBoundLiteralControl anchorText = (DataBoundLiteralControl) dataGridItem.Cells[1].Controls[0];
						int incLength =anchorText.Text.IndexOf("><")- (anchorText.Text.IndexOf("IncidentId=")+"IncidentId=".Length+2);
						int IncNum=Int32.Parse(anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength));







int IncNum=Int32.Parse(anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength));





异常详情:System.FormatException:输入字符串格式不正确。



任何帮助请



Exception Details: System.FormatException: Input string was not in a correct format.

Any help please

推荐答案

能怎样? Int32.Parse 对于任意字符串都不能成功。不是很明显吗?您可以随时验证它并从此异常中恢复,您可以通过将调用放入一些具有适当异常处理的try-catch块来处理它,或者更好的是,只需使用 Int32.TryParse 相反:

https ://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx [ ^ ]。



有趣的是,如果您阅读.NET BCL源代码,您会看到 TryParse 方法不在内部使用异常处理,相反,它们会阻止抛出例外。



-SA
How else? Int32.Parse cannot be successful for any arbitrary string. Isn't it obvious? You can always validate it and recover from this exception is you handle it by putting the call in some try-catch block with appropriate exception handling, or, better, just using Int32.TryParse instead:
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

Interestingly, if you read .NET BCL source code, you will see that TryParse methods don't use exception handling internally, instead, they prevent throwing exceptions.

—SA


这篇关于输入字符串的格式不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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