Input string was not in a correct format

笑着哭i 提交于 2019-12-23 05:50:10

问题


Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 30: lbl_userName.Text = objReader.Item(0) & " " & objReader.Item(1)
Line 31: lbl_resumeHead.Text = objReader.Item(3)
Line 32: lbl_experience.Text = Convert.ToInt32(objReader.Item(4))

How to display an Integer value from the table.


回答1:


Convert.ToInt32(objReader.Item(4))

This assumes that the value coming from the database is convertable to an integer. If it's an string that can't be parsed, DbNull, etc. it will fail.

More examples in the documentation.




回答2:


Your objReader.Item(4) does not contain a valid integer value - it may be DBNull.Value, String.Empty, a floating point value or something else:

Convert.ToInt32(objReader.Item(4))

By the way - instead of using ordinals you should use field names - this ensure you are using the correct field.

If you know the field name, use it:

Convert.ToInt32(objReader("MyIntegerField"))


来源:https://stackoverflow.com/questions/10156547/input-string-was-not-in-a-correct-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!