2010-10-06 116 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