登陆按钮后台代码为:
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();//关闭数据库
        
        }
    }
}运行后错误为:未将对象引用设置到对象的实例。说明: 执行当前 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

解决方案 »

  1.   

    ConfigurationManager.ConnectionStrings["connection"]这个不会为NULL了吧,断点看看
      

  2.   

    ConfigurationManager.ConnectionStrings["connection"].ConnectionString
    这货在Web.config里面看看是不是数据库连接有没有连上
      

  3.   

    ConfigurationManager.ConnectionStrings["connection"].ConnectionString
    //你配置文件不存在connection节点
      

  4.   

    SqlCommand cmd = new SqlCommand("select * from UserInfo where userID="+ txt_usetname.Text +"", conn);sql语句拼接有问题SqlCommand cmd = new SqlCommand("select * from UserInfo where userID='"+ txt_usetname.Text +"'", conn);
      

  5.   

    看到了我当年的身影  兄弟 加油吧。     哥已经不用ado.net很多年。  都用linq啊Nhiberate啊神马的, ORM框架比ado.net方便多了
      

  6.   

    SqlCommand cmd = new SqlCommand("select * from UserInfo where userID='"+ txt_usetname.Text +"'", conn);
    加两个引号啦。。varchar类型