Exemplo 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();
            }
        }
Exemplo n.º 2
0
        private void MetroButtonSaveModule_Click(object sender, EventArgs e)
        {
            int    modId        = Convert.ToInt32(metroLabelModuleId.Text);
            string moduleName   = metroTextBoxModuleTitle.Text;
            string moduleLeader = metroTextBoxModuleLeader.Text;

            //validate not to duplicate modules
            if (!IsValidModule(moduleName))
            {
                MetroMessageBox.Show(this, "Module with the name " + moduleName + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (moduleName != "")
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    if (modId == 0)
                    {
                        TblModule module = new TblModule
                        {
                            Title        = moduleName,
                            ModuleLeader = moduleLeader
                        };
                        context.TblModules.Add(module);

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

                        MetroMessageBox.Show(this, moduleName + " Module Added Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        var moduleToUpdate = context.TblModules.SingleOrDefault(module => module.ModuleId == modId);
                        if (moduleToUpdate != null)
                        {
                            moduleToUpdate.Title        = moduleName;
                            moduleToUpdate.ModuleLeader = moduleLeader;
                        }
                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception es)
                        {
                            MetroMessageBox.Show(this, es.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        MetroMessageBox.Show(this, moduleName + " Updated Successfully!", "Successful Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                InitializeModuleList();
            }
        }
        private void MetroButtonSaveRole_Click(object sender, EventArgs e)
        {
            int    rolId    = Convert.ToInt32(metroLabelRoleId.Text);
            string roleName = MetroTextBoxRoleName.Text;

            //validate not to duplicate Roles
            if (!IsValidRole(roleName))
            {
                MetroMessageBox.Show(this, "Role with the name " + roleName + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (roleName != "")
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    if (rolId == 0)
                    {
                        TblRole role = new TblRole
                        {
                            RoleName = roleName
                        };
                        context.TblRoles.Add(role);

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

                        MetroMessageBox.Show(this, roleName + " Role Added Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        var roleToUpdate = context.TblRoles.SingleOrDefault(role => role.RoleId == rolId);
                        if (roleToUpdate != null)
                        {
                            roleToUpdate.RoleName = roleName;
                        }
                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception es)
                        {
                            MetroMessageBox.Show(this, es.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        MetroMessageBox.Show(this, roleName + " Updated Successfully!", "Successful Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                InitializeRoleList();
            }
        }
        private void UpdateMember()
        {
            using (var context = new KucsaManagementDatabaseEntities())
            {
                var memberToUpdate = context.TblMembers.SingleOrDefault(member => member.MemberId == memId);
                if (memberToUpdate != null)
                {
                    memberToUpdate.MemberName = memberName;

                    //did not want to make it possible to change the registration number
                    memberToUpdate.MemberRegistration = memberToUpdate.MemberRegistration;
                    memberToUpdate.Password           = memberPass;
                    memberToUpdate.MemberThumbnail    = bigPicture;
                    memberToUpdate.MemberBigPicture   = bigPicture;
                    memberToUpdate.RoleId             = Convert.ToInt32(MetroComboBoxRole.SelectedValue);
                    memberToUpdate.CourseId           = Convert.ToInt32(MetroComboBoxMemberCourse.SelectedValue);
                    memberToUpdate.ModuleId           = Convert.ToInt32(MetroComboBoxMemberModule.SelectedValue);
                }
                try
                {
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    MetroMessageBox.Show(this, "Could not update the user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                MetroMessageBox.Show(this, memberName + " Updated Successfully but registration number remained!", "Successful Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void MetroButtonDelete_Click(object sender, EventArgs e)
        {
            int deleteID = Convert.ToInt32(metroLabelMemberId.Text);

            if (metroLabelMemberId.Text != "0" && metroLabelMemberId.Text != null && isValidDelete(deleteID))
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    var delUser = new TblMember {
                        MemberId = deleteID
                    };
                    context.TblMembers.Attach(delUser);
                    context.TblMembers.Remove(delUser);
                    context.SaveChanges();
                    MetroMessageBox.Show(this, delUser.MemberName + " Removed Successfully from the system!", "Successful Removal", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    LoadMembers();
                }
            }
        }
        private void AddNewMember()
        {
            using (var context = new KucsaManagementDatabaseEntities())
            {
                TblMember member = new TblMember
                {
                    MemberName         = memberName,
                    MemberRegistration = memberReg,
                    Password           = memberPass,
                    MemberThumbnail    = bigPicture,
                    MemberBigPicture   = bigPicture,
                    RoleId             = Convert.ToInt32(MetroComboBoxRole.SelectedValue),
                    CourseId           = Convert.ToInt32(MetroComboBoxMemberCourse.SelectedValue),
                    ModuleId           = Convert.ToInt32(MetroComboBoxMemberModule.SelectedValue)
                };

                if (!isValidRegistrationNo(memberReg))
                {
                    MetroMessageBox.Show(this, "The registration number entered already exists!", "Unsuccessful Operation!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    context.TblMembers.Add(member);

                    try
                    {
                        context.SaveChanges();
                        MetroMessageBox.Show(this, memberName + " Added Successfully as a member!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    catch (Exception)
                    {
                        MetroMessageBox.Show(this, "Could not save a new user", "Unsuccessful Operation!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }