2017-01-11 4 views
0

Login.aspxのVisual Studioの2つの層データベース(DAL)

protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     String tbNRIC = txtLogUsername.Text.ToString(); 
     String tbPassword = txtLogPassword.Text.ToString(); 

     PatientDAO fmTd = new PatientDAO(); 
     fmTd.getPatientByNricPassword(tbNRIC, tbPassword); 

     if(?? > 0) 
     { 
      Response.Redirect("Home.aspx"); 
     } 
     else 
     { 

     } 
    } 

どのように私は、ユーザー名とパスワードが一致した場合にチェックした後のResponse.Redirect?

PatientDAO.cs

public int getPatientByNricPassword(String tbNRIC, String tbPassword) 
     { 
      string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 

      SqlDataAdapter da; 
      DataSet ds = new DataSet(); 
      Patient mypatient = new Patient(); 

      StringBuilder sqlStr = new StringBuilder(); 

      int result = 0; 

      sqlStr.AppendLine("Select * from Login where"); 
      sqlStr.AppendLine("tbNRIC = @paratbNRIC AND tbPassword = @paratbPassword"); 

      try 
      { 
       SqlConnection myConn = new SqlConnection(DBConnect); 
       da = new SqlDataAdapter(sqlStr.ToString(), myConn); 

       da.SelectCommand.Parameters.AddWithValue("@paratbNRIC", tbNRIC); 
       da.SelectCommand.Parameters.AddWithValue("@paratbPassword", tbPassword); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 
      } 
      catch (SqlException ex) 
      { 
       logManager log = new logManager(); 
       log.addLog("PatientDAO.getPatientByNricPassword", sqlStr.ToString(), ex); 
       mypatient = null; 
      } 
      return result; 
     } 

どのように私はlogin.aspxのために結果を渡すのですか?

Patient.csユーザー名とパスワードの一致とLogin.aspxのResponse.Redirectをどうかを確認するためにPatientDAOから確認方法

public class Patient 
    { 
     public string tbNRIC { get; set; } 
     public string tbPassword { get; set; } 
    } 

答えて

0

私が見ることができるコードにいくつかの問題があります。

最初の方法getPatientByNricPasswordは、変数を返すようですが、これは割り当てられていません。おそらく、その場合のレコードの数を返すことになります。

第2に、資格情報の詳細でPatientDAO fmTd = new PatientDAO();を初期化していません。 適切な値を初期化する - textプロパティがすでに文字列であるとして、あなたはToString()を配置する必要はありません

PatientDAO fmTd = new PatientDAO(); 
fmTd.tbNRIC = tbNRIC; // which is txtLogUsername.Text 
fmTd.tbPassword = tbPassword ; //which is txtLogPassword.Text 

注意。

第三に、あなたは、さらに次のようにif状態で確認することができ、DAL法の結果を格納する例recordCountための変数を持っている必要があります -

var recordCount = fmTd.getPatientByNricPassword(tbNRIC, tbPassword); 
if(recordCount > 0) 
    { 
    Response.Redirect("Home.aspx"); 
    } 
関連する問題