vb.net 输入字符串的格式不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10156547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:03:41  来源:igfitidea点击:

Input string was not in a correct format

vb.netsql-server-2005

提问by Tim Medora

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

输入字符串的格式不正确。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.FormatException:输入字符串的格式不正确。

源错误:

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

How to display an Integervalue from the table.

如何显示Integer表中的值。

回答by Oded

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:

objReader.Item(4)不包含有效的整数值 - 它可能是DBNull.Value, String.Empty, 浮点值或其他内容:

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

回答by Tim Medora

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.

这假设来自数据库的值可转换为整数。如果它是一个无法解析的字符串DbNull,等等,它就会失败。

More examples in the documentation.

文档中的更多示例。