+// Page Load
    
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser();
            Guid uid = (Guid)user.ProviderUserKey;
            var ss = db.SecretSantas.SingleOrDefault(x => x.UserID == uid);

            if (!Page.IsPostBack)
            {
                LoginName ln = new LoginName();

                //if you have an account and have chosen a brownley
                // display brownley along with their wishlist
                //ideally edit your own wish list but may not have time


                //if brownley is null then logging in for first time
                if (ss.BrownleyID != null)
                {
                    historyPanel.Visible = true;
                    DisplayAccountInfo(ss.BrownleyID, ss.Name);
                    txtWishList.Focus();
                }
                else
                {
                    btnChoose.Text = "Spin the Brownley Wheel";

                }
            }

            else
            {

            }

            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;
            SetCalendar(day, month);

            HtmlGenericControl h = (HtmlGenericControl)vwLogin.FindControl("loginname");
            h.InnerHtml = ss.Name + "!";


            //  var ss = db.SecretSantas.SingleOrDefault(x => x.UserID == uid);
            var brownleyRow = db.Brownleys.SingleOrDefault(x => x.Name == ss.Name);

            txtWishList.Text = brownleyRow.WishList;

        }
      
    
   
+// Set Calendar
    
       public void SetCalendar(int day, int month)
        {
            //convert day and month int to string to add to div
            string strMonth = Convert.ToString(month);
            string strDay = Convert.ToString(day);
            switch (strMonth)
            {

                case "1":
                    strMonth = "Jan";
                    break;
                case "2":
                    strMonth = "Feb";
                    break;
                case "3":
                    strMonth = "Mar";
                    break;
                case "4":
                    strMonth = "Apr";
                    break;
                case "5":
                    strMonth = "May";
                    break;
                case "6":
                    strMonth = "Jun";
                    break;
                case "7":
                    strMonth = "Jul";
                    break;
                case "8":
                    strMonth = "Aug";
                    break;
                case "9":
                    strMonth = "Sep";
                    break;
                case "10":
                    strMonth = "Oct";
                    break;
                case "11":
                    strMonth = "Nov";
                    break;
                case "12":
                    strMonth = "Dec";
                    break;

            }

            divmonth.InnerText = strMonth;
            divday.InnerText = strDay;
        }
    
    
    
+// Spin
    
      protected void spin(Object sender, EventArgs e)
        {

            //thescruffsterDataContext db = new thescruffsterDataContext();
            staticImg.Style.Add("visibility", "hidden");
            pnlMessage.Style.Add("visibility", "hidden");
            //ensure that secretsanta does not spin wheel again!
            MembershipUser user = Membership.GetUser();
            Guid uid = (Guid)user.ProviderUserKey;
            var ss = db.SecretSantas.SingleOrDefault(x => x.UserID == uid);

            if (ss.BrownleyID != null)
            {
                DisplayAccountInfo(ss.BrownleyID, ss.Name);
                return;
            }
            //  keep the wheel turning
            string script = "__doAutoSave();";
            ScriptManager.RegisterStartupScript(this, this.GetType(),
                "myTimer", script, true);
            btnChoose.Text = "My Account";
        }
    
    
    
+//Web Methods
           
             
            
                [WebMethod]
        public static string ViewHistory()
        {        
            SecretSantaData ssData = new SecretSantaData();
            string wl = ssData.GetBrownley()._2009_Wish_List;
            return wl;
        }
       
        [WebMethod]
        public static string ViewHistory2010()
        {

           SecretSantaData ssData = new SecretSantaData();
           string wl = ssData.GetBrownley()._2010_Wish_List;
           return wl;
        }

        [WebMethod]
        public static string ViewHistory2011()
        {
            SecretSantaData ssData = new SecretSantaData();
            string wl = ssData.GetBrownley()._2011_Wish_List;
            return wl;
        }

        [WebMethod]
        public static string ViewHistory2012()
        {
            SecretSantaData ssData = new SecretSantaData();
            string wl = ssData.GetBrownley()._2012_Wish_List;
            return wl;
        }

        [WebMethod]
        public  static string EditWishList(string text)
        {
            try
            {
                thescruffsterDataContext db = new thescruffsterDataContext();
                if (!String.IsNullOrEmpty(text))
                {
                    // this satisfies GetBrownley()
                    SecretSantaData ssData = new SecretSantaData();
                    var uid = ssData.GetUserID();
                    var ss = db.SecretSantas.SingleOrDefault(x => x.UserID == uid);                   
                    var brownleyRow = db.Brownleys.SingleOrDefault(x => x.Name == ss.Name);
                    brownleyRow.WishList = text;
                    

                    try
                    {
                        brownleyRow.WishList = text;
                        db.SubmitChanges();
                        text = brownleyRow.WishList;
                    }

                    catch (Exception dbex) { return "Error: " + dbex.Message.ToString(); }

                    //if brownley is already chosen, find their secret santa and send an email
                    //containing wish list


                    if (brownleyRow.IsChosen)
                    {
                        var ssEmail = db.SecretSantas.SingleOrDefault(x => x.BrownleyID == brownleyRow.BrownleyID).Email;
                        var ssName = db.SecretSantas.SingleOrDefault(x => x.BrownleyID == brownleyRow.BrownleyID).Name;
                        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                        System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();

                        //send an email to secretsanta when wishlist is created or updated
                        System.Net.Mail.MailAddress mailFrom = new System.Net.Mail.MailAddress("secretsanta@thescruffster.com");
                        System.Net.Mail.MailAddress mailTo = new System.Net.Mail.MailAddress(ssEmail);
                        message.IsBodyHtml = true;
                        message.To.Add(mailTo);
                        message.Bcc.Add(new System.Net.Mail.MailAddress("angie@thescruffster.com"));                        
                        message.Subject = "Secret Santa Wish List";
                        message.From = mailFrom;
                        message.Body = Hi " + ssName + "! " + brownleyRow.Name + " has updated their wish list:
                        You can also login to your account to view: Brownley Secret Santa
                        Merry Christmas!
                        secretsanta@thescruffster.com
                        mailClient.Send(message);
                        message.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MembershipUser user = Membership.GetUser();

                Mishap m = new Mishap
                { 
                    Date = DateTime.Now,
                    Error = ex.Message.ToString(),
                    UserName = user.UserName
                };

                try
                {
                    using (thescruffsterDataContext db = new thescruffsterDataContext())
                    {
                        db.Mishaps.InsertOnSubmit(m);
                        db.SubmitChanges();
                    }
                }
                catch (Exception except)
                {
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                    System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
                    var Email = "angie@thescruffster.com";

                    System.Net.Mail.MailAddress mailFrom = new System.Net.Mail.MailAddress("secretsanta@thescruffster.com");
                    System.Net.Mail.MailAddress mailTo = new System.Net.Mail.MailAddress(Email);
                    message.IsBodyHtml = true;
                    message.To.Add(mailTo);

                    message.Subject = "Secret Santa Edit WishList Error";
                    message.From = mailFrom;
                    message.Body = except.StackTrace 
                    mailClient.Send(message);
                    message.Dispose();
                }

            }
            return text;
        }
           
            
+// Data Context
       
        partial class thescruffsterDataContext
         {
          private static string _secretsanta;

        private static thescruffsterDataContext db
        {
            get { return new thescruffsterDataContext(); }
        }

        // partial class for dbml
       public thescruffsterDataContext()
        : base(ConfigurationManager.ConnectionStrings["thescruffsterConnectionString"].ConnectionString, mappingSource)
           {
               OnCreated();
           } 
    }
       
       
+// Account Info
       
        public void DisplayAccountInfo(int? brownleyID, string name)
        {
            try
            {
                thescruffsterDataContext db = new thescruffsterDataContext();

                //get name of brownley
                var brownley = db.Brownleys.SingleOrDefault(x => x.BrownleyID == brownleyID);

                //get recipient wish list
                string b = brownley.Name;
                string w = brownley.WishList;
                if (String.IsNullOrEmpty(w))
                {
                    w = b + " has not yet submitted a wish list.";
                }

                // get your wish list
                var myrow = db.Brownleys.SingleOrDefault(y => y.Name == name);

                string mywish = myrow.WishList;
                if (String.IsNullOrEmpty(mywish))
                {
                    mywish = "No wish list submitted";
                }
                //make wheel and button invisible
                //add secret santa account info
                accountpanel.Visible = true;
                lblMyAccount.Visible = true;
                mywishpanel.Visible = true;
                historyPanel.Visible = true;
                staticImg.Style.Add("visibility", "hidden");
                btnChoose.Visible = false;
                recepientname.InnerHtml = b;
                recepientwishlabel.InnerHtml = b + "'s Wish List:";
                recepientwishdata.InnerHtml = w;
                txtWishList.Text = mywish;
            }


            catch (Exception e)
            {
                thescruffsterDataContext db = new thescruffsterDataContext();
                MembershipUser user = Membership.GetUser();

                Mishap m = new Mishap
                {
                    Date = DateTime.Now,
                    Error = e.Message.ToString(),
                    UserName = user.UserName

                };

                try
                {
                    db.Mishaps.InsertOnSubmit(m);
                    db.SubmitChanges();
                }
                catch (Exception except)
                {
                    try
                    {
                       System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                    System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
                    var Email = "angie@thescruffster.com";

                    System.Net.Mail.MailAddress mailFrom = new System.Net.Mail.MailAddress("secretsanta@thescruffster.com");
                    System.Net.Mail.MailAddress mailTo = new System.Net.Mail.MailAddress(Email);
                    message.IsBodyHtml = true;
                    message.To.Add(mailTo);

                    message.Subject = "Secret Santa Edit WishList Error";
                    message.From = mailFrom;
                    message.Body = except.StackTrace
                    mailClient.Send(message);
                    message.Dispose();
                    }

                    catch (Exception ex)
                    {
                        
                    }
                }

            }

        }