未将对象引用设置为对象的实例 #5 [英] Object reference not set to an instance of an object #5
问题描述
sUsername.Trim();sPassword.Trim();string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;SqlConnection myConnection = new SqlConnection(ConnectionString);
<块引用>
未将对象引用设置为对象的实例.说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以了解有关错误及其在代码中的来源的更多信息.
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.
有什么想法吗?我不明白错误.
好吧,您还没有显示它出现在哪一行.它表明发生了其中之一:
sUsername
为空sPassword
为空WebConfigurationManager.ConnectionStrings["dbnameConnectionString"]
返回 null
顺便说一句,像这样调用 Trim()
作为一个语句是没有意义的.字符串是不可变的 - Trim()
返回 修剪后的版本.你想要这样的东西:
sUsername = sUsername.Trim();sPassword = sPassword.Trim();
...但只有在检查它们是否为空之后.
sUsername.Trim();
sPassword.Trim();
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object.
Any ideas? I don't understand the error.
Well, you haven't shown which line it occurs on. It suggests that one of these occurred:
sUsername
was nullsPassword
was nullWebConfigurationManager.ConnectionStrings["dbnameConnectionString"]
returned null
Btw, calling Trim()
as a statement on its own like that is pointless. Strings are immutable - Trim()
returns the trimmed version. You want something like:
sUsername = sUsername.Trim();
sPassword = sPassword.Trim();
... but only after checking whether they're null or not.
这篇关于未将对象引用设置为对象的实例 #5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!