程序是这样的:
  string auid = tbName.Text.ToString();
        int buid = Convert.ToInt32(db.src("select * from admin where uid='" + auid + "'"));
        string apwd = tbPwd.Text.ToString();
        int bpwd = Convert.ToInt32(db.src("select * from admin where pwd='" + apwd + "'"));        if (buid > 0 && bpwd > 0)
        {
            Session["qx"] = db.src("select qx from admin where uid='" + auid + "'");
            Session["uid"] = db.src("select uid from admin where uid='" + auid + "'");
            Response.Redirect("admin.aspx");
        }
        else
        {
            if (buid <= 0)
          
               Label1.Text  = "用户名错误";
           
            if (bpwd <= 0)
            
                Label1.Text = Label1.Text + "&密码错误";
            
        }
-------------------------------
db.src是这样的:
public static string src(string que)
    {
        SqlConnection con = db.con();
        con.Open();
        SqlCommand cmd = new SqlCommand(que, con);
        return cmd.ExecuteScalar().ToString();
        con.Close();
    }
----------------------------------
当输入正确的用户名密码时可以顺利跳转到目标页,当输入不正确的用户名密码时,则报错,如下:
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 84:         con.Open();
行 85:         SqlCommand cmd = new SqlCommand(que, con);
行 86:         return cmd.ExecuteScalar().ToString();
行 87:         con.Close();
行 88:     }
 
哪里出错了?请各位高手帮帮忙!!!

解决方案 »

  1.   

    第一,我输入张三的用户名和李四的密码也是可以登陆的
    第二,如果用户名或密码不存在,你转换成int的时候,可能会报错的
      

  2.   

    select * from admin where uid='" + auid + "'"
    应该改成
    select count(*) from admin where uid='" + auid + "'"然后用户名和密码应该对应起来在数据库中查找记录
      

  3.   

    string auid = tbName.Text.ToString();
    int buid = Convert.ToInt32(db.src("select count(*) from admin where uid='" + auid + "'"));if(buid > 0)
    {
        string apwd = tbPwd.Text.ToString();
        int bpwd = Convert.ToInt32(db.src("select count(*) from admin where  uid='"+auid+"'and pwd='" + apwd + "'"));
        if( bpwd > 0)
         {
           Session["qx"] = db.src("select qx from admin where uid='" + auid + "'");
           Session["uid"] = db.src("select uid from admin where uid='" + auid + "'");
           Response.Redirect("admin.aspx");
         }
         else
         { 
               Label1.Text = Label1.Text + "&密码错误";
         }}
    else
    {
            Label1.Text  = "用户名错误";
    }