Input string was not in a correct format

2019-06-11 23:07发布

问题:

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"))