Object reference not set to an instance of an object #5
1 2 3 4 | 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.
有什么想法吗?我不明白这个错误。
好吧,你还没有显示它发生在哪条线上。这表明其中一种情况发生了:
sUsername 为空sPassword 为空WebConfigurationManager.ConnectionStrings["dbnameConnectionString"] 返回空
顺便说一句,把
1 2 | sUsername = sUsername.Trim(); sPassword = sPassword.Trim(); |
…但只有在检查它们是否为空之后。
嗯,我确实理解,但你错过了推荐信。错误发生在哪里?
1 2 3 4 5 | Line 30: sUsername.Trim(); Line 31: sPassword.Trim(); Line 32: string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString; Line 33: SqlConnection myConnection = new SqlConnection(ConnectionString); Line 34: try |
如果我假设有痉挛的存在-而且恐惧……那么connectionString"dbnameconnectionString"是否存在于web.config中?如果不是,则为空,.connectionString自然会引发该错误。
第30行和第31行不做任何事情:
1 2 | sUsername = sUsername.Trim(); sPassword= sPassword.Trim(); |
发布错误发生的位置
它发生的原因是任何一个变量都为空。您可以在调试(运行时)期间检查
这只是意味着您正试图访问一个空引用的成员;也就是说,这里的一个变量是