2014-09-28 96 views
-3

我的代碼是假設檢查是密碼和用戶名是否正確。儘管它沒有做任何事情。我無法弄清楚爲什麼。我的代碼沒有任何反應

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"); 


     } 
+2

這麼多,我不知道從哪裏開始一點毛病此代碼! – Fredou 2014-09-28 02:57:08

+0

-1無法繼續呈現此問題的方式。需要更多關於預期會發生什麼,發生了什麼(明顯發生某些事情),發生什麼不該發生的事情等詳細信息... – 2014-09-28 02:58:40

+0

基本上,當您單擊Button然後button_login_click會觸發。這是在一個單獨的頁面上。 – 2014-09-28 03:04:44

回答

0

checkUserNPass,你是不正確映射的用戶名commUser到密碼tempPassword

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