当前位置: 代码迷 >> ASP.NET >> 小弟再做一个登陆页面 但是出以下异常 希望能得到各位的指导
  详细解决方案

小弟再做一个登陆页面 但是出以下异常 希望能得到各位的指导

热度:5569   发布时间:2013-02-25 00:00:00.0
小弟再做一个登陆页面 但是出以下错误 希望能得到各位大虾的指导
登陆按钮后台代码为:[size=16px][/size]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btn_login_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new
  SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
  SqlCommand cmd = new SqlCommand("select * from UserInfo where userID="+ txt_usetname.Text +"", conn);
  try
  {
  conn.Open();
  SqlDataReader sdr = cmd.ExecuteReader();
  //如果用户名输入正确
  if (sdr.Read())
  {
  if (sdr["userPwd"].ToString() == txt_userpwd.Text) //如果用户密码正确
  {
  conn.Close();//关闭数据可连接
  Session["userID"] = txt_usetname.Text.Trim();//将用户id放在session中
  Response.Redirect("Default.aspx");//进入 主界面
  }
  else
  {
  Response.Write("lbl_tishi.Text='密码错误'");
  }
  }
  else//否则
  {
  Response.Write("lbl_tishi.Text='用户名错误'");
  }
  }
  catch (System.Exception ee)
  {
  Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
  }
  finally
  {
  conn.Close();//关闭数据库
   
  }
  }
}

运行后错误为:[size=16px][/size]

未将对象引用设置到对象的实例

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

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

源错误: 


行 17: protected void btn_login_Click(object sender, EventArgs e)
行 18: {
行 19: SqlConnection conn = new
行 20: SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
行 21: SqlCommand cmd = new SqlCommand("select * from UserInfo where userID="+ txt_usetname.Text +"", conn);

源文件: e:\StuManage\UserLogin.aspx.cs 行: 19 

堆栈跟踪: 


[NullReferenceException: 未将对象引用设置到对象的实例。]
  Default2.btn_login_Click(Object sender, EventArgs e) in e:\StuManage\UserLogin.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

------解决方案--------------------------------------------------------
看下数据库连接字符串获取到了吗
------解决方案--------------------------------------------------------
ConfigurationManager.ConnectionStrings["connection"]这个不会为NULL了吧,断点看看
  相关解决方案