コード例 #1
0
        private void frmScheduleCurriculumCourseWizard_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (mustSaveItems)
            {
                using (var Dbconnection = new MCDEntities())
                {
                    using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction())
                    {
                        try
                        {
                            SelectedSechuledParameters SSP = GetSelectedSchedule();
                            if (SSP != null)
                            {
                                //CRUD Operations
                                CurrentScheduleConfiguration.FacilitatorID          = SSP.FacilitatorID;
                                CurrentScheduleConfiguration.ScheduleStartDate      = SSP.StartDate.Date;
                                CurrentScheduleConfiguration.ScheduleCompletionDate = SSP.EndDate;
                                if (CurrentScheduleConfiguration.ScheduleLocationID == (int)EnumScheduleLocations.Onsite)
                                {
                                    CurrentScheduleConfiguration.OnSiteSchedule = new OnSiteSchedule()
                                    {
                                        DateLastModified = DateTime.Now,
                                        VenueID          = SSP.VenueID
                                    };
                                }
                                if (CurrentScheduleConfiguration.ScheduleLocationID == (int)EnumScheduleLocations.OffSite)
                                {
                                    CurrentScheduleConfiguration.OffSiteSchedule = new OffSiteSchedule()
                                    {
                                        DateLastModified = DateTime.Now,
                                        AddressID        = SSP.VenueID
                                    };
                                }
                                Dbconnection.Entry(CurrentScheduleConfiguration).State = EntityState.Added;
                                // CurrentSelectedCurriculumCourseEnrollment.Schedules.Add();
                                Dbconnection.CurriculumCourseEnrollments.Attach(CurrentSelectedCurriculumCourseEnrollment);
                                CurrentSelectedCurriculumCourseEnrollment.LookupEnrollmentProgressStateID = (int)EnumEnrollmentProgressStates.In_Progress;
                                Dbconnection.Entry(CurrentSelectedCurriculumCourseEnrollment).Reference(a => a.LookupEnrollmentProgressState).Load();
                                Dbconnection.Entry(CurrentSelectedCurriculumCourseEnrollment).State = EntityState.Modified;
                            }

                            ////saves all above operations within one transaction
                            Dbconnection.SaveChanges();
                            //commit transaction
                            dbTran.Commit();
                        }
                        catch (Exception ex)
                        {
                            if (ex is DbEntityValidationException)
                            {
                                foreach (DbEntityValidationResult entityErr in ((DbEntityValidationException)ex).EntityValidationErrors)
                                {
                                    foreach (DbValidationError error in entityErr.ValidationErrors)
                                    {
                                        MessageBox.Show(error.ErrorMessage, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            //Rollback transaction if exception occurs
                            dbTran.Rollback();
                        }
                    }
                };
            }
        }
コード例 #2
0
        private SelectedSechuledParameters GetSelectedSchedule()
        {
            SelectedSechuledParameters Rtn = null;

            foreach (DataGridViewRow row in dgvAvailableOnSiteCoursesToSchedule.Rows)
            {
                var chkValue = row.Cells[colAvailableDateToScheduleSelectCourse.Index].Value;
                if (chkValue != null)
                {
                    if ((Boolean)chkValue)
                    {
                        AvailableOnSitePeriods AOSP = (AvailableOnSitePeriods)availableOnSitePeriodsBindingSource.Current;
                        Rtn = new SelectedSechuledParameters()
                        {
                            VenueID       = AOSP.VenueID,
                            FacilitatorID = AOSP.FacilitatorID,
                            StartDate     = AOSP.CourseStartDate,
                            EndDate       = AOSP.CourseEndDate
                        };
                    }
                }
            }
            foreach (DataGridViewRow row in dgvAvailableOffSiteCoursesToSchedule.Rows)
            {
                var chkValue = row.Cells[colAvailableOffSiteDateToScheduleSelectCourse.Index].Value;
                if (chkValue != null)
                {
                    if ((Boolean)chkValue)
                    {
                        AvailableOffSitePeriods AOSP = (AvailableOffSitePeriods)availableOffSitePeriodsBindingSource.Current;
                        Rtn = new SelectedSechuledParameters()
                        {
                            VenueID       = AOSP.VenueID,
                            FacilitatorID = AOSP.FacilitatorID,
                            StartDate     = AOSP.CourseStartDate,
                            EndDate       = AOSP.CourseEndDate
                        };
                    }
                }
            }
            foreach (DataGridViewRow row in dgvCurrentlyScheduledDates.Rows)
            {
                var chkValue = row.Cells[colCurrentlySechuldedCourseSelector.Index].Value;
                if (chkValue != null)
                {
                    if ((Boolean)chkValue)
                    {
                        CurrentlyScheduledCourses CSC = (CurrentlyScheduledCourses)currentlyScheduledCoursesBindingSource.Current;
                        Rtn = new SelectedSechuledParameters()
                        {
                            VenueID       = CSC.VenueID,
                            FacilitatorID = CSC.FacilitatorID,
                            StartDate     = CSC.CourseStartDate,
                            EndDate       = CSC.CourseEndDate
                        };
                    }
                }
            }


            return(Rtn);
        }