Esempio n. 1
0
        private void MetroButtonSaveCourse_Click(object sender, EventArgs e)
        {
            int    cosId       = Convert.ToInt32(metroLabelCourseId.Text);
            string courseName  = metroTextBoxCourseName.Text;
            string facultyName = metroTextBoxFaculty.Text;

            //validate not to duplicate modules
            if (!IsValidCourse(courseName))
            {
                MetroMessageBox.Show(this, "Course with the name " + metroTextBoxCourseName.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (courseName != "")
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    if (cosId == 0)
                    {
                        TblCourse course = new TblCourse
                        {
                            CourseName    = courseName,
                            CourseFaculty = facultyName
                        };
                        context.TblCourses.Add(course);

                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception es)
                        {
                            MetroMessageBox.Show(this, es.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        MetroMessageBox.Show(this, courseName + " Course Added Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        var courseToUpdate = context.TblCourses.SingleOrDefault(course => course.CourseId == cosId);
                        if (courseToUpdate != null)
                        {
                            courseToUpdate.CourseName    = courseName;
                            courseToUpdate.CourseFaculty = facultyName;
                        }
                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception es)
                        {
                            MetroMessageBox.Show(this, es.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        MetroMessageBox.Show(this, courseName + " Updated Successfully!", "Successful Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                InitializeCourseList();
            }
        }
        private void MetroButtonDelete_Click(object sender, EventArgs e)
        {
            int deleteID = Convert.ToInt32(metroLabelCourseId.Text);

            if (metroLabelCourseId.Text != "0" && metroLabelCourseId.Text != null && isValidDelete(deleteID))
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    var delCourse = new TblCourse {
                        CourseId = deleteID
                    };
                    context.TblCourses.Attach(delCourse);
                    context.TblCourses.Remove(delCourse);
                    context.SaveChanges();
                    MetroMessageBox.Show(this, delCourse.CourseName + " Removed Successfully from the system!", "Successful Removal", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    InitializeCourseList();
                }
            }
        }