protected void btnlogin_Click(object sender, EventArgs e)
    {
        string name = txtname.ToString().Trim();
        string pass = txtpassword.ToString().Trim();
        using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StaffMs;Integrated Security=True"))
        {
               SqlCommand com = new SqlCommand("select * from dbo.admin where ad_name=@a and password=@b",con);
                com.Parameters.Add(new SqlParameter("@a",name));
                com.Parameters.Add(new SqlParameter("@b",pass));
                con.Open();
               SqlDataReader dr=com.ExecuteReader();
                if(dr.Read())
                 {
                     Response.Redirect("Default.aspx");
                 }
                else
                 {
                     
                    // Response.Write("用户名或密码输入错误");
                     Response.Write("<script>alert('用户名或密码输入错误')</script>");
                    
                   
                 }
                con.Close();
          
         }
    }用户名和密码都是对的,但是就是提示“用户名或密码输入错误”,大家帮忙看看啊!

解决方案 »

  1.   

    if(dr.hasRow)
    {
       Response.Redirect("Default.aspx");}
    else{
        Response.Write("<script>alert('用户名或密码输入错误')</script>");}
    有个hasrow把,你代码写的……
      

  2.   

     表单验证?  直接查能行么?
     //
            // Summary:
            //     Verifies that the specified user name and password exist in the data source.
            //
            // Parameters:
            //   username:
            //     The name of the user to validate.
            //
            //   password:
            //     The password for the specified user.
            //
            // Returns:
            //     true if the specified username and password are valid; otherwise, false.
            public abstract bool ValidateUser(string username, string password);
      

  3.   

    要么你就用asp.net的登录控件  自己写的控件不会给你自己验证
      

  4.   

    dr.hasRow 在外面加个判断 ,判断在查出来的datareader 是否存在数据行 没有自然是失败
      

  5.   


                   SqlDataReader dr=com.ExecuteReader();
    参数化查询 '(@a nvarchar(4000),@b nvarchar(33))select * from dbo.admin where' 需要参数 @a,但未提供该参数。
    看不懂哪里出错了!
      

  6.   

    试过了不行啊!
    SqlDataReader dr=com.ExecuteReader();
                    if(dr.HasRows)
                     {
                         if (dr.ToString() != "")
                         {
                             Response.Redirect("Default.aspx");
                         }
                         else
                         { 
                         }
                        
                     }
                    else
                     {
                         
                        // Response.Write("用户名或密码输入错误");
                         Response.Write("<script>alert('用户名或密码输入错误')</script>");
                        
                       
                     }
      

  7.   

    select * from dbo.admin where ad_name=@a and password=@b
    这样写SQL     会被注入
      

  8.   


    protected void btnlogin_Click(object sender, EventArgs e)
        {
            string name = txtname.ToString().Trim();
            string pass = txtpassword.ToString().Trim();
            using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StaffMs;Integrated Security=True"))
            {
                   SqlCommand com = new SqlCommand("select count(*) from dbo.admin where ad_name=@a and password=@b",con);
                    com.Parameters.Add(new SqlParameter("@a",name));
                    com.Parameters.Add(new SqlParameter("@b",pass));
                    con.Open();
                    int n = (int)Comm.ExecuteScalar();
                    if(n>0)                 {
                         Response.Redirect("Default.aspx");
                     }
                    else
                     {
                         
                        // Response.Write("用户名或密码输入错误");
                         Response.Write("<script>alert('用户名或密码输入错误')</script>");
                        
                       
                     }
                    con.Close();
              
             }
        }
      

  9.   

    int n = (int)com.ExecuteScalar();
    用户代码为处理
    未将对象引用设置到对象的实例。
      

  10.   


    ...... com.Parameters.Add(new SqlParameter("@a", SqlType.VarChar,20));
            com.Parameters["@a"].Value = name;
            com.Parameters.Add(new SqlParameter("@b", SqlType.VarChar, 20));
            com.Parameters["@b"].Value = pass;
    ........
      

  11.   

    1、你的数据库所在系统实例是默认实例吗?如果不是,把Data Source=.修改为Data Source=./实例名
    2、将你的sql输出出来,放到查询分析器里看看,是否有数据。
      

  12.   

    select * from dbo.admin where ad_name=@a and password=@b
    用户可以这样写 select * from dbo.admin where ad_name=haha and password=12154 and 1=1
      

  13.   

     int n = (int)Comm.ExecuteScalar();
    改成Executenonquery()看看
      

  14.   

    protected void btnlogin_Click(object sender, EventArgs e)
        {
            string name = txtname.ToString().Trim();
            string pass = txtpassword.ToString().Trim();改成
    protected void btnlogin_Click(object sender, EventArgs e)
        {
            string name = txtname.TextTrim();
            string pass = txtpassword.Text.Trim();
    解决了!这样运行就好了!呵呵。谢谢大家的关注啊!