2010-10-06 59 views
0

我有一個表單,用戶可以訂閱和取消訂閱我的電子郵件列表。到目前爲止,我有訂閱按鈕工作正常「添加成員」功能。現在我需要幫助我的「刪除成員」功能(取消訂閱按鈕)。它將允許用戶從數據庫中刪除他們的記錄。當我運行代碼並單擊「取消訂閱」按鈕時,我無法獲取正確的邏輯,以便它將刪除用戶的記錄(如果存在)。謝謝你的幫助!刪除記錄onClick,asp.net

這裏是我使用的訂閱和退訂按鈕-----------

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 


    public partial class joinmailinglist : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void addMember(object sender, EventArgs e) 
     { 
      // here you are defining the classes for the database and the linq 
      mailinglistClassDataContext Class = new mailinglistClassDataContext(); 
      mailinglistMember member = new mailinglistMember(); 

      // Now we are going to add the data to the member 
      // Here we are going to let the system define a GUID for the unique user ID 
      member.memberID = new Guid(); 

      // here we are going to capture the user inputs and we are going to set these to lower case especially the email so that we can do a proper comparison later. 
      member.fname = txtFirstName.Text; 
      member.lname = txtLastName.Text; 
      member.email = txtEmail.Text; 

      // Here we are going to create the URL so we can later remove the user if they decide to opt out. 
      member.removeurl = "http://removeuser.aspx?code=" + member.memberID.ToString(); 

      // Here we are going to use a LINQ query to search the class of mailinglistmembers for any emails that contain equal values of the text field and select it. 
      var duplicatecheck = from emails in Class.mailinglistMembers 
           where emails.email.Contains(txtEmail.Text) 
           select emails; 

      // Here we are going to check that the count of duplicate is equal to zero. If so then we are going to insert the member information into the class and then submit the changes to the database. 
      if (duplicatecheck.Count() == 0) 
      { 
       Class.mailinglistMembers.InsertOnSubmit(member); 
       Class.SubmitChanges(); 

      } 
      else 
      { 
       lblDuplicate.Text = "Hey you have already entered your information."; 
      } 
     } 


protected void deleteMember(object sender, EventArgs e) 
    { 



     // here you are defining the classes for the database and the linq 
     mailingListClassDataContext Class = new mailingListClassDataContext(); 
     mailinglistMember member = new mailinglistMember(); 



     // here we are going to capture the user inputs and we are going to set these to lower case especially the email so that we can do a proper comparison later. 

     member.email = txtEmail.Text; 


     // Here we are going to use a LINQ query to search the class of mailinglistmembers for any emails that contain equal values of the text field and select it. 

         var deleterec = from emails in Class.mailinglistMembers 
         where emails.email.Contains(txtEmail.Text) 
          select emails; 

     // Here we check if the record exisits 

     if (deleterec.Count() == 0) 
     { 
      Class.mailinglistMembers.DeleteOnSubmit(member); 
      Class.SubmitChanges(); 
      Response.Redirect("frm_confirmation.aspx"); 

     } 
     else 
     { 
      lblDelete.Text = "No record exsists!"; 
     } 
    } 
} 

回答

0

嘗試下面的代碼。

string mailAddress = txtEmail.Text.Trim().ToLower(); 

using (var db = new mailingListClassDataContext()) 
{ 
    var records = from e in db.mailinglistMembers 
        where e.mail == mailAddress 
        select e; 

    if (records != null) 
    { 
     db.mailinglistMembers.DeleteAllOnSubmit(records); 
     db.SubmitChanges(); 
     Response.Redirect("frm_confirmation.aspx"); 
     Response.End(); 
    } 
    else 
    { 
     lblDelete.Text = "No records exists!"; 
    } 
} 
+0

當我跑這個,我可以在db.SubmitChanges()的錯誤;說明「序列包含多個元素」:/ – PW2 2010-10-07 15:41:26

+0

@ PW2:這意味着您在'mailinglistMembers'中有重複的條目。我對代碼進行了一些更改,以將其考慮在內。 – sshow 2010-10-07 15:46:28

+0

我應用此代碼並得到相同的錯誤。列表中的電子郵件是唯一的。所以奇怪的是它給了我這個錯誤。 – PW2 2010-10-07 15:53:04

0

您的意思是不是這樣做的代碼:

    var deleterec = Class.mailinglistMembers 
        .FirstOrDefault(emails => emails.email.Contains(txtEmail.Text)); 

    if (deleterec != null) 
    { 
     Class.mailinglistMembers.DeleteOnSubmit(deleterec); 
     Class.SubmitChanges(); 
     Response.Redirect("frm_confirmation.aspx"); 

    } 
+0

我跑你的代碼。我在「Class.SubmitChanges();」上找到「row not found」 – PW2 2010-10-06 15:21:38

+0

你有在數據庫中定義的主鍵嗎?如果沒有,你應該。 – 2010-10-06 16:23:31

+0

是的,我有主鍵。 – PW2 2010-10-07 06:41:05

0

看起來像有人試圖添加到我原創的代碼項目文章中發佈的代碼。不確定您是否閱讀過這篇文章,但它可能有助於解決您的問題,並瞭解它是如何工作的。鏈接會將您返回到可以捕獲GUID的刪除頁面。我使用GUID作爲標識符來刪除用戶。 Original Article