在填充数据库表中的登录页面信息后发生错误

时间:2013-11-19 15:13:43

标签: asp.net visual-studio-2010

对象引用未设置为对象的实例。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

Source Error: 


Line 17:     {
Line 18:         SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=class;Integrated Security=True");
Line 19:         cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString;
Line 20:         cnn.Open();
Line 21:         SqlCommand cmd = new SqlCommand();

Source File: c:\Users\VIJU\Documents\Visual Studio 2010\WebSites\Class\Default2.aspx.cs    Line: 19 

堆栈追踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Default2.Button1_Click(Object sender, EventArgs e) in c:\Users\VIJU\Documents\Visual Studio 2010\WebSites\Class\Default2.aspx.cs:19
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

2 个答案:

答案 0 :(得分:0)

我的猜测是这一行:

cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString

我打赌sqlconnection不存在,或者在你的web.config中拼写错误。

答案 1 :(得分:0)

我认为您的连接字符串键名存在问题。 更正如下:

cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString;