views:

55

answers:

2

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

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

    }
jarrett
i ran your code. i get "row not found" on line "Class.SubmitChanges();"
PW2
Do you have a primary key defined in your database? If not, you should.
jarrett
yes, i have primary key.
PW2
A: 

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!";
    }
}
sshow
when i ran this, i can an error on db.SubmitChanges(); stating "sequence contains more than one element" :/
PW2
@PW2: This means you have duplicate entries in `mailinglistMembers`. I made some changes to the code to take this into account.
sshow
i applied this code and got the same error. the emails in the list are unique. so it's strange that it's giving me this error.
PW2
@PW2: I'm positive you are not getting the `Sequence contains more than one element` error with the current code.
sshow
i ran it twice...and got the same error. i'll keep troubleshooting..thanks so much for your help.
PW2
ok, i just realized that the db had two records with the same memberid. i cleared the dublicate record and reran the code, now i'm getting a `"row not found or changed"` error at db.submit changes();
PW2