public int RemoveTimeSlot(TimeSlot timeSlot)
        {
            int count = 0;

            try
            {
                DBConnection.OpenConnection();

                SqlCommand cmd = new SqlCommand(CommonConstants.QUERY_REMOVE_TIMESLOT, DBConnection.DatabaseConnection);



                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, timeSlot.GetDay_of_the_Week());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_START_TIME, timeSlot.GetStart_Time());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_END_TIME, timeSlot.GetEnd_Time());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_TYPE, timeSlot.GetSlotType());



                count = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                try
                {
                    Console.WriteLine(ex);
                    DBConnection.CloseConnection();
                }
                catch (Exception)
                {
                    throw;
                }
                return(-1);
            }
            finally
            {
                try
                {
                    DBConnection.CloseConnection();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(count);
        }
        public int SaveTimeSlot(TimeSlot timeSlots)
        {
            int count = 0;


            try
            {
                DBConnection.OpenConnection();



                SqlCommand cmd2 = new SqlCommand(CommonConstants.QUERY_GET_TIMESLOT, DBConnection.DatabaseConnection);

                cmd2.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, timeSlots.GetDay_of_the_Week());
                cmd2.Parameters.AddWithValue(CommonConstants.PARAMETER_START_TIME, timeSlots.GetStart_Time());
                cmd2.Parameters.AddWithValue(CommonConstants.PARAMETER_END_TIME, timeSlots.GetEnd_Time());
                cmd2.Parameters.AddWithValue(CommonConstants.PARAMETER_TYPE, timeSlots.GetSlotType());

                SqlDataReader reader = cmd2.ExecuteReader();

                if (reader.Read())
                {
                    reader.Close();
                    DBConnection.CloseConnection();
                    return(0);
                }



                reader.Close();

                DBConnection.CloseConnection();

                if (timeSlots.GetSlotType().Trim() == "Lunch Break")
                {
                    DBConnection.OpenConnection();
                    SqlCommand cmd3 = new SqlCommand(CommonConstants.QUERY_GET_LUNCH_BREAK_COUNT_FOR_THE_DAY, DBConnection.DatabaseConnection);

                    cmd3.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, timeSlots.GetDay_of_the_Week());
                    cmd3.Parameters.AddWithValue(CommonConstants.PARAMETER_TYPE, timeSlots.GetSlotType());

                    SqlDataReader reader2 = cmd3.ExecuteReader();



                    reader2.Read();

                    if (int.Parse(reader2[CommonConstants.COLUMN_BREAK_COUNT].ToString()) >= 1)
                    {
                        reader2.Close();
                        DBConnection.CloseConnection();
                        return(-2);
                    }



                    DBConnection.CloseConnection();
                }



                DBConnection.OpenConnection();

                SqlCommand cmd = new SqlCommand(CommonConstants.QUERY_SAVE_TIMESLOT, DBConnection.DatabaseConnection);

                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_DAY_OF_THE_WEEK, timeSlots.GetDay_of_the_Week());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_START_TIME, timeSlots.GetStart_Time());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_END_TIME, timeSlots.GetEnd_Time());
                cmd.Parameters.AddWithValue(CommonConstants.PARAMETER_TYPE, timeSlots.GetSlotType());


                count = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                try
                {
                    Console.WriteLine(ex);
                    DBConnection.CloseConnection();
                }
                catch (Exception)
                {
                    throw;
                }

                return(-1);
            }
            finally
            {
                try
                {
                    DBConnection.CloseConnection();
                }
                catch (Exception)
                {
                    throw;
                }
            }



            return(count);
        }
Beispiel #3
0
        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);
                }
            }
        }