我有一个表单,用户可以订阅和取消订阅我的电子邮件列表。到目前为止,我有订阅按钮工作正常“添加成员”功能。现在我需要帮助我的“删除成员”功能(取消订阅按钮)。它将允许用户从数据库中删除他们的记录。当我运行代码并单击“取消订阅”按钮时,我无法获取正确的逻辑,以便它将删除用户的记录(如果存在)。谢谢你的帮助!删除记录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!";
}
}
}
当我跑这个,我可以在db.SubmitChanges()的错误;说明“序列包含多个元素”:/ – PW2 2010-10-07 15:41:26
@ PW2:这意味着您在'mailinglistMembers'中有重复的条目。我对代码进行了一些更改,以将其考虑在内。 – sshow 2010-10-07 15:46:28
我应用此代码并得到相同的错误。列表中的电子邮件是唯一的。所以奇怪的是它给了我这个错误。 – PW2 2010-10-07 15:53:04