检查

2012-03-29 40 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