Ejemplo n.º 1
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 MetroButtonDelete_Click(object sender, EventArgs e)
        {
            int deleteID = Convert.ToInt32(metroLabelModuleId.Text);

            if (metroLabelModuleId.Text != "0" && metroLabelModuleId.Text != null && isValidDelete(deleteID))
            {
                using (var context = new KucsaManagementDatabaseEntities())
                {
                    var delModule = new TblModule {
                        ModuleId = deleteID
                    };
                    context.TblModules.Attach(delModule);
                    context.TblModules.Remove(delModule);
                    context.SaveChanges();
                    MetroMessageBox.Show(this, delModule.Title + " Removed Successfully from the system!", "Successful Removal", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    InitializeModuleList();
                }
            }
        }