protected void btnLogin_Click(object sender, EventArgs e)
    {        SqlConnection conn = new SqlConnection("Server = (local);user id = sa;pwd = 1;database = Login");
        try
        {
            conn.Open();
            string cmdText = "select username from userinfo where username = '" + this.txtUsername.Text.Trim() + "' and userpassword='" + this.txtPassword.Text.Trim() + "'";            SqlDataAdapter sqlCommon = new SqlDataAdapter(cmdText, conn);
            DataSet ds = new DataSet();
            
          sqlCommon.Fill(ds);
          if (txtUsername.Text.Trim() == ds.Tables[0].Rows[0][" username"] && txtPassword.Text.Trim() == ds.Tables[0].Rows[0]["userpassword"])                        {
                conn.Close();
                Response.Redirect("Default2.aspx");
               
            }
            else
            {
               lblResults.Visible = true;
               lblResults.Text = "用户名或密码错误!";
               txtUsername.Text = "";
               txtPassword.Text = "";            }
            conn.Close();
        }
        catch (SqlException ex)
        {
            Console.WriteLine("连接出错" + ex.Message);
        }
    } 
结果是密码错的, lblResults也不会提示,只是txtPassword.Text = "";密码对的也会出现txtPassword.Text = "";而不是转业
   

解决方案 »

  1.   

    你在sql字符串那设个断点调试下 看看cmdText 字符串能不能在sql server里面查找到数据不哦
      

  2.   

    txtUsername.Text.Trim() == ds.Tables[0].Rows[0][" username"] && txtPassword.Text.Trim() == ds.Tables[0].Rows[0]["userpassword"]txtUsername.Text.Trim() == ds.Tables[0].Rows[0][" username"].ToString() && txtPassword.Text.Trim() == ds.Tables[0].Rows[0]["userpassword"].ToString();看看加了tostring(),行不行!
      

  3.   

    cmdText
    在设计模式中无法计算表达式。