public int SaveWorkingHours(WorkHours day) { int count = 0; try { DBConnection.OpenConnection(); SqlCommand cmd = new SqlCommand(CommonConstants.QUERY_SAVE_WORK_HOURS, DBConnection.DatabaseConnection); SqlCommand cmd2 = new SqlCommand(CommonConstants.QUERY_REMOVE_TIMESLOTS_BY_DAY, DBConnection.DatabaseConnection); cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_START_TIME, day.GetStart_Time()); cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_END_TIME, day.GetEnd_Time()); cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, day.GetDay_of_the_Week()); cmd2.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, day.GetDay_of_the_Week()); count = cmd.ExecuteNonQuery(); int count2 = cmd2.ExecuteNonQuery(); if (count2 == -1 || count == -1) { count = -1; } } catch (Exception ex) { try { Console.WriteLine(ex); DBConnection.CloseConnection(); } catch (Exception) { throw; } return(-1); } finally { try { DBConnection.CloseConnection(); } catch (Exception) { throw; } } return(count); }
private void ButtonSave_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; if (daysListBox.SelectedItem == null) { MessageBox.Show("Please Enter Required Fields", "Validation Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { WorkHours workHours = cntrl.GetWorkHoursByDay(daysListBox.SelectedItem.ToString()); DateTime startTime = DateTime.Parse(workHours.GetStart_Time()); DateTime endTime = DateTime.Parse(workHours.GetEnd_Time()); TimeSlot timeSlots = new TimeSlot(); timeSlots.SetDay_of_the_Week(daysListBox.SelectedItem.ToString()); timeSlots.SetStart_Time(startTimePicker.Value.ToLongTimeString()); if (radioButtonThirtyMinutes.Checked == true) { timeSlots.SetEnd_Time(startTimePicker.Value.AddMinutes(30).ToLongTimeString()); } else if (radioButtonOneHour.Checked == true) { timeSlots.SetEnd_Time(startTimePicker.Value.AddHours(1).ToLongTimeString()); } if (radioButtonLunchBreak.Checked == true) { timeSlots.SetSlotType(radioButtonLunchBreak.Text.ToString()); } else if (radioButtonWorkTime.Checked == true) { timeSlots.SetSlotType(radioButtonWorkTime.Text.ToString()); } DateTime timeslot_startTime = DateTime.Parse(timeSlots.GetStart_Time()); DateTime timeslot_endTime = DateTime.Parse(timeSlots.GetEnd_Time()); bool status; if (startTime.Hour < timeslot_startTime.Hour && endTime.Hour > timeslot_endTime.Hour) { status = true; } else if ((startTime.Hour == timeslot_startTime.Hour && endTime.Hour > timeslot_endTime.Hour) && startTime.Minute <= timeslot_startTime.Minute) { status = true; } else if ((startTime.Hour < timeslot_startTime.Hour && endTime.Hour == timeslot_endTime.Hour) && endTime.Minute >= timeslot_endTime.Minute) { status = true; } else if ((startTime.Hour == timeslot_startTime.Hour && endTime.Hour == timeslot_endTime.Hour) && (startTime.Minute <= timeslot_startTime.Minute && endTime.Minute >= timeslot_endTime.Minute)) { status = true; } else { status = false; } if (status) { int count = cntrl.SaveTimeSlot(timeSlots); Cursor.Current = Cursors.Default; if (count >= 1) { MessageBox.Show("TimeSlot Saved SuccessFully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (count == 0) { MessageBox.Show("TimeSlot Already Added", "TimeSlot Exist", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (count == -1) { MessageBox.Show("Error Occurred", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (count == -2) { MessageBox.Show("Cannot add another Lunch Break for " + timeSlots.GetDay_of_the_Week() + "!!", "Duplicate Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Cursor.Current = Cursors.WaitCursor; LoadData(); Cursor.Current = Cursors.Default; } else { MessageBox.Show("Please Enter Valid Time-Slot", "Invalid Time-Slot", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }