我的代码没有任何反应

时间:2014-09-28 02:50:41

标签: c# asp.net

我的代码是检查密码和用户名是否正确。虽然它没有做任何事情。我无法弄清楚原因。

 public virtual void checkUserNPass(string uNameExists, string pNameExists)
   {

       SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); //creates sql connection object  to the database via connection string 
       conn.Open();

       string checkUserQuery = "select count (*) from users where UserName='" + uNameExists + "'"; //UserName query
       string checkPasswordQuery = "select count (*) from users where Password='" + pNameExists + "'"; //Password query

       SqlCommand commUser = new SqlCommand(checkUserQuery, conn);
       SqlCommand commPass = new SqlCommand(checkPasswordQuery, conn);

       int tempUser = Convert.ToInt32(commUser.ExecuteScalar().ToString());
       int tempPassword = Convert.ToInt32(commUser.ExecuteScalar().ToString());

       string user = commUser.ExecuteScalar().ToString().Replace(" ", "");
       string password = commUser.ExecuteScalar().ToString().Replace(" ", "");
       conn.Close();

       if (tempUser != 0)
       {
           conn.Open();

           if (tempPassword != 0)
           {
               conn.Open();
               HttpContext.Current.Session["newUserSession"] = uNameExists;
               HttpContext.Current.Response.Redirect("Manager.aspx");
               conn.Close();
           }//verifypassword
           else
           {
               HttpContext.Current.Response.Write("Password is incorrect.");
               conn.Close();
           }
       }
      else
       {
                HttpContext.Current.Response.Write("Username is incorrect.");
                conn.Close();
       }
       conn.Close();

       }
   }


}


    protected void Button_Login_Click(object sender, EventArgs e)
    {

        string uNameExists = login_username.Text;
        string pNameExists = login_password.Text;

        NotLoggedIn checkLoginObj = new NotLoggedIn();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); //creates sql connection object  to the database via connection string 
        conn.Open(); //Open Connection

        string checkuserQuery = "select count(*) from users where UserName='" + uNameExists + "'"; //UserName textbox ID
        string checkPasswordQuery = "select count (*) from users where Password='" + pNameExists + "'";

        SqlCommand comm = new SqlCommand(checkuserQuery, conn);  //Command that allows connection
        SqlCommand commPass = new SqlCommand(checkPasswordQuery, conn);
        conn.Close();

        if (!string.IsNullOrEmpty(uNameExists) && !string.IsNullOrEmpty(pNameExists))
        {
            checkLoginObj.checkUserNPass(uNameExists, pNameExists);
        }
        else
        {
            HttpContext.Current.Response.Write("Not working");


        }

1 个答案:

答案 0 :(得分:0)

checkUserNPass中,您错误地将用户名commUser映射到密码tempPassword

int tempPassword = Convert.ToInt32(commUser.ExecuteScalar().ToString());