檢查

2012-03-29 39 views
0

我有一個小問題把我帶了一天,直到如今它不解決,我想保存未在數據庫中存在的記錄,如果用戶輸入已經存在的話,將不保存,但事情是它的工作原理不知何故後來我發現,當我試圖驗證第2行輸入喜歡的用戶名或電子郵件熱潮相同的值!數據被插入,導致重複。如何解決這個問題?你能幫我嗎?在這裏感謝檢查

是我的代碼。

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     lblInternetAccount.Value = lblAccountNo.Text.ToString() + txtUsername.Text.ToString(); 
     DataSet ds = new DataSet(); 
     ds = (startWebService.getAllUsers()); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      foreach (DataRow drow in ds.Tables[0].Rows) 
      { 
       string username = drow["UserName"].ToString(); 
       string acctNo = drow["AccountNumber"].ToString(); 

       if (username != txtUsername.Text.ToString() || acctNo != lblAccountNo.Text.ToString()) 
       { 
        startWebService.insertUser(lblAccountNo.Text.ToString(), txtUsername.Text.ToString(), txtPassword.Text.ToString(), txtUsername.Text.ToString(), cboQuestion.Text.ToString(), txtAnswer.Text.ToString(), lblInternetAccount.Value.ToString(), txtPassword.Text.ToString()); 
        lblMessage.Text = "Record Updated!"; 

       } 
       else 
       { 
        lblMessage.Text = "<br>Unable to create record because account number/email is already registered.Please login instead.<br><br>"; 

       } 
      } 
     } 
    } 

Web服務:

private DataSet GetDataSet(string strSPROC) 
    { 

     SqlConnection conn = new SqlConnection(connectionString); 
     SqlCommand cmd = conn.CreateCommand(); 
     cmd.CommandText = strSPROC; 
     conn.Open(); 
     SqlDataAdapter myDataAdapter = new SqlDataAdapter(); 
     myDataAdapter.SelectCommand = cmd; 
     DataSet dsMT = new DataSet(); 
     myDataAdapter.Fill(dsMT); 
     return dsMT; 
     conn.Close(); 
    } 
    [WebMethod] 
    public void insertUser(string accountNo, string userName, string pAssword, string eMail, string secretQuestion, string secretAnswer,string onlineActNo,string acctkey) 
    { 
     Insert("ELMS_CREATEMEMBER", accountNo, userName, pAssword, eMail, secretQuestion, secretAnswer, onlineActNo,acctkey); 
    } 

回答

1

你是幾乎沒有。您只需執行實際插入即可(這將導致許多插入)。

你在做什麼是這樣的:

爲每個現有的記錄 - > 如果記錄不匹配的新紀錄 - > 插入新記錄

你需要做的是什麼先檢查所有現有的記錄,也只有這樣,當沒有記錄匹配的新記錄,插入新記錄。

一個修改後的代碼示例:

lblInternetAccount.Value = lblAccountNo.Text.ToString() + txtUsername.Text.ToString(); 
DataSet ds = new DataSet(); 
ds = (startWebService.getAllUsers()); 

bool isDuplicated = false; 
if (ds.Tables[0].Rows.Count > 0) 
{ 
    foreach (DataRow drow in ds.Tables[0].Rows) 
    { 
     string username = drow["UserName"].ToString(); 
     string acctNo = drow["AccountNumber"].ToString(); 

     if (username == txtUsername.Text.ToString() && acctNo == lblAccountNo.Text.ToString()) 
     { 
      isDuplicated = true; 
     } 

    } 

    if (!isDuplicated) 
    { 
     startWebService.insertUser(lblAccountNo.Text.ToString(), txtUsername.Text.ToString(), txtPassword.Text.ToString(), txtUsername.Text.ToString(), cboQuestion.Text.ToString(), txtAnswer.Text.ToString(), lblInternetAccount.Value.ToString(), txtPassword.Text.ToString()); 
     lblMessage.Text = "Record Updated!"; 
    } 
    else 
    { 
     lblMessage.Text = "<br>Unable to create record because account number/email is already registered.Please login instead.<br><br>"; 
    } 
} 
+0

仍無法正常工作,記錄已經存在,但系統再次保存它。 – Dhenn 2012-03-29 04:36:04

+0

@Dhenn - 固定的代碼,有一個小bug – Polity 2012-03-29 05:06:50

+0

行會,我會跟蹤它 – Dhenn 2012-03-29 05:31:16