开发者

delete record onClick, asp.net

I have a form where users can subscribe and unsubcribe to my email list. so far, i have the subscribe button working fine "add member" function. Now i need help with my "delete member " function (unsubscribe button). it will allows the user to delete their record from the database. When I run the code and click the "unsubscribe" button, i can't get the logic correct so that it will delete the user's record if it exisit. thanks for your help!

here's the code i'm using for the subscribe and unsubscribe buttons -----------

    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 pr开发者_运维问答oper 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!";
        }
    }
}


Try the below code.

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!";
    }
}


You may have meant to do this:

                    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");

    }


Looks like someone tried to add on to the code I origianlly posted in my article on code project. Not sure if you've read the article but it might help solve your problem and understand how it was intended to work. A link would return you to a removal page that would and capture the GUID. I used the GUID as the identifyer to remove the user. Original Article

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜