Exemple #1
0
        protected void AddBtn_Click(object sender, EventArgs e)
        {
            using (WebhostEntities db = new WebhostEntities())
            {
                int      studentId = Convert.ToInt32(StudentSelectComboBox.SelectedValue);
                MedTimes mt        = new MedTimes()
                {
                    morning = MorningCB.Checked,
                    lunch   = LunchCB.Checked,
                    dinner  = DinnerCB.Checked,
                    bedtiem = BedtimeCB.Checked
                };

                Student student = db.Students.Where(s => s.ID == studentId).Single();

                int     mid     = MedsSections["Morning"];
                Section morning = db.Sections.Where(sec => sec.id == mid).Single();
                int     lid     = MedsSections["Lunch"];
                Section lunch   = db.Sections.Where(sec => sec.id == lid).Single();
                int     did     = MedsSections["Dinner"];
                Section dinner  = db.Sections.Where(sec => sec.id == did).Single();
                int     bid     = MedsSections["Bedtime"];
                Section bedtime = db.Sections.Where(sec => sec.id == bid).Single();
                if (mt.morning)
                {
                    morning.Students.Add(student);
                }
                else if (morning.Students.Contains(student))
                {
                    morning.Students.Remove(student);
                }

                if (mt.lunch)
                {
                    lunch.Students.Add(student);
                }
                else if (lunch.Students.Contains(student))
                {
                    lunch.Students.Remove(student);
                }

                if (mt.dinner)
                {
                    dinner.Students.Add(student);
                }
                else if (dinner.Students.Contains(student))
                {
                    dinner.Students.Remove(student);
                }

                if (mt.bedtiem)
                {
                    bedtime.Students.Add(student);
                }
                else if (bedtime.Students.Contains(student))
                {
                    bedtime.Students.Remove(student);
                }

                db.SaveChanges();
            }

            LoadTable();
        }