2010-10-22 89 views
0

我的代碼:值不通過從文本框代碼

區檢查什麼登錄按鈕recordExist做

private void btnLogin_Click(object sender, EventArgs e) 
    { 
     if (this.txtUserName.Text == "" || this.txtPassword.Text == "") 
     { 
      MessageBox.Show("Credentials are missing...", clsVariables._strProjectName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     else 
     { 
      string _strQuery = "SELECT AtNumConstructorID, TxtConstructorName AS Fullname, tblConstructorDetails.txtUserName FROM tblConstructorDetails WHERE txtUserName = '" + this.txtUserName.Text + "' AND TxtPassword LIKE '" + this.txtPassword.Text + "' "; 
      if (clsFunctions.recordExist(_strQuery, "tblConstructorDetails") == true) 
      { 
       clsVariables._sTimeLogin = DateTime.Now.ToLongTimeString();//recording the time of login in the CES 
       long totalRow = 0; 
       //Set the Data Adapter 
       OleDbDataAdapter da = new OleDbDataAdapter(_strQuery, clsConnections._olbedbCN); 
       DataSet ds = new DataSet(); // creating a dataset to enter the values in the dataadapter 
       da.Fill(ds, "tblConstructorDetails"); 
       totalRow = ds.Tables["tblConstructorDetails"].Rows.Count - 1; 
       clsVariables._sContId = Convert.ToInt32(ds.Tables["tblConstructorDetails"].Rows[0].ItemArray.GetValue(0)); 
       clsVariables._sConstructor = ds.Tables["tblConstructorDetails"].Rows[0].ItemArray.GetValue(1).ToString(); 
       clsVariables._sUserID = ds.Tables["tblConstructorDetails"].Rows[0].ItemArray.GetValue(2).ToString(); 
       clsUserLogs.RecordLogin(clsVariables._sTimeLogin, clsVariables._sContId); 
       clsApplication._boolAPP_CONNECTED = true; 
       this.Close(); 

      } 
      #region trash code 2 
      /*string _strUsername = this.txtUserName.Text.Trim(); 
      string _strPassword = txtPassword.Text; 
      string _strSQL = "select * from tblConstructorDetails where Upper(TxtUserName) = '" + _strUsername.ToUpper() +"' and TxtPassword = '" + _strPassword + "'"; 
      if() 
      { 

      } */ 
      #endregion 

      else 
      { 
       clsVariables._intAttempt--; 
       if (clsVariables._intAttempt > 0) 
       { 
        MessageBox.Show("Login failed. Please try again. Only " + Convert.ToString(clsVariables._intAttempt) + " attempts remaining...", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
       else 
       { 
        MessageBox.Show("Exiting the system", clsVariables._strProjectName, MessageBoxButtons.OK); 
        this.Close(); 
       } 
      } 
     } 
    } 
    #endregion 

代碼:

#region Filters the records if exists in the table 
    //Filter if Recort is Exist in the Table. 
    public static bool recordExist(string _sSQL, string _sTable) 
    { 
     long totalRow = 0; 
     //Set the Data Adapter 
     OleDbDataAdapter _oledbAdDa = new OleDbDataAdapter(_sSQL, _olbedbCN); 
     DataSet _ds = new DataSet(); 
     _oledbAdDa.Fill(_ds, _sTable); 
     totalRow = Convert.ToInt32(_ds.Tables[_sTable].Rows.Count); 
     if (totalRow > 0) { return true; } 
     else { return false; } 
    } 
    #endregion 

我現在面臨的問題是,當我嘗試在相應的文本框中輸入值,該值沒有傳遞給查詢以檢查登錄是否可用....

請檢查相同,爲我提供可能的解決方案和理由,爲什麼這件事情正在發生的事情....提前

感謝名單....

+0

爲什麼不刪除所有不相關的代碼。問題在於,即使當文本在文本框中時,頂部的if語句也是正確的? - 這也會阻止任何人告訴你不要像這樣建立sql字符串,因爲有人會來一起做令人討厭的事情。 – 2010-10-22 10:37:17

回答

0

你可能要更改的字符串比較運營商在你的SQL從

=

LIKE

我得到的感覺你錯過了問你的問題,真的問題是沒有返回數據的SQL。

string _strQuery = "SELECT AtNumConstructorID, TxtConstructorName AS Fullname, tblConstructorDetails.txtUserName FROM tblConstructorDetails WHERE txtUserName LIKE '" 
+ this.txtUserName.Text + "' AND TxtPassword LIKE '" + this.txtPassword.Text + "' "; 

該變化正好在第一行的結尾處。

http://xkcd.com/327/