private void btnRemoveReligionDates_Click(object sender, EventArgs e) { try { RELIGIONID = _context.Religions.Where(j => j.Name == cbxReligionName.Text).Select(s => s.ReligionID).First(); if (_context.ReligionHolidays.Where(j => (j.ReligionID == RELIGIONID) && j.ReligionHolidaysDate == dateTimePicker1.Value.Date).Select(s => s.ReligionHolidaysDate).Count() != 0) { if (MessageBox.Show("Do you really want to delete this date?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { RELIGIONHOLIDAYSID = _context.ReligionHolidays.Where(j => (j.ReligionID == RELIGIONID) && j.ReligionHolidaysDate == dateTimePicker1.Value.Date).Select(s => s.ReligionHolidaysID).First(); System.Data.EntityKey religionKey = new System.Data.EntityKey("MojaEntities.ReligionHolidays", "ReligionHolidaysID", RELIGIONHOLIDAYSID); var religionHolidayDelete = _context.GetObjectByKey(religionKey); _context.DeleteObject(religionHolidayDelete); _context.SaveChanges(); MessageBox.Show("You have successfully deleted religion from database.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); TOTALRELIGIONDAYS = _context.Religions.Where(w => w.Name == cbxReligionName.Text).Select(s => s.TotalReligiousDays).First(); TOTALRELIGIONDAYS -= 1; var Religion = _context.Religions.First(k => k.ReligionID == RELIGIONID); Religion.TotalReligiousDays = TOTALRELIGIONDAYS; txtTotalReligionDays.Text = "" + TOTALRELIGIONDAYS.ToString(); _context.SaveChanges(); RefreshDates(); } } } catch { MessageBox.Show("Delete operation was not successful. Please, contact your administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void RefreshDates() { RELIGIONID = _context.Religions.Where(w => w.Name == cbxReligionName.Text).Select(s => s.ReligionID).First(); ListaRELIGIONHOLIDAY = _context.ReligionHolidays.Where(j => j.ReligionID == RELIGIONID).ToList(); dgReligionHolidays.DataSource = ListaRELIGIONHOLIDAY; TOTALRELIGIONDAYS = _context.Religions.Where(w => w.Name == cbxReligionName.Text).Select(s => s.TotalReligiousDays).First(); txtTotalReligionDays.Text = "" + TOTALRELIGIONDAYS.ToString(); }