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