protected void SubmitButton_Click(object sender, EventArgs e)
        {
            lastcallEntities db = new lastcallEntities();
            subscriber       s  = (from x in db.subscribers where x.email == email.Text select x).FirstOrDefault();

            if (s != null)
            {
                ErrorMessage.Text = "Username is already registered";
                return;
            }

            // The new subscriber record
            s = new subscriber()
            {
                deliveryaddress = address.Text,
                email           = email.Text,
                emailoffers     = (byte)(emailoffers.Checked ? 1 : 0),
                friendlyname    = friendlyname.Text,
                onmailinglist   = (byte)(mailinglist.Checked ? 1 : 0),
                password        = password.Text,
                phone           = telephone.Text,
                textoffers      = (byte)(textoffers.Checked ? 1 : 0),
            };

            string[] userprefs = foodprefs.Value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            // The subscriber's food preferences
            foodpreference [] preferences = new foodpreference[userprefs.Length];

            for (int i = 0; i < userprefs.Length; i++)
            {
                preferences[i] = new foodpreference()
                {
                    subscriberid = s.id,
                    preferenceid = Int32.Parse(userprefs[i])
                };
            }

            // Extremely wonderful Entity Framework goodness. The associated foodpreferences records will be created as the subscriber record is saved.
            s.foodpreferences = preferences;

            db.subscribers.Add(s);
            db.SaveChanges();
            db.Dispose();
        }
Ejemplo n.º 2
0
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            lastcallEntities db = new lastcallEntities();
            subscriber       s  = (from x in db.subscribers where x.email == email.Text select x).FirstOrDefault();

            if (s != null)
            {
                ErrorMessage.Text = "Username is already registered";
                return;
            }

            s = new subscriber()
            {
                deliveryaddress = address.Text,
                email           = email.Text,
                emailoffers     = (byte)(emailoffers.Checked ? 1 : 0),
                friendlyname    = friendlyname.Text,
                onmailinglist   = (byte)(mailinglist.Checked ? 1 : 0),
                password        = password.Text,
                phone           = telephone.Text,
                textoffers      = (byte)(textoffers.Checked ? 1 : 0)
            };

            db.subscribers.Add(s);
            db.SaveChanges();

            string[] prefs = foodprefs.Value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string p in prefs)
            {
                foodpreference pref = new foodpreference()
                {
                    subscriberid = s.id,
                    preferenceid = Int32.Parse(p)
                };
                db.foodpreferences.Add(pref);
            }

            db.SaveChanges();
        }