private void bt_SavePlan_Click(object sender, EventArgs e) { using (AxaContext ctx = new AxaContext()) { if (tb_Description.Text == "") { MessageBox.Show("Please filled up all fields"); } else { if (dataGridView_Plan.SelectedRows.Count > 0) { int tempIdplan = Int32.Parse(dataGridView_Plan.SelectedRows[0].Cells[0].Value.ToString()); var planUpdate = new Plan { PlanId = tempIdplan, Description = tb_Description.Text, PlanType = cb_PlanType.SelectedItem.ToString(), Category = cb_PlanCategory.SelectedItem.ToString(), }; ctx.Plans.Attach(planUpdate); ctx.Entry(planUpdate).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); ClearTextField(); AddEditPlan_Load(null, null); MessageBox.Show("Plan Succesfully Updated"); } else { var plan = new Plan { Description = tb_Description.Text, PlanType = cb_PlanType.SelectedItem.ToString(), Category = cb_PlanCategory.SelectedItem.ToString(), }; ctx.Plans.Add(plan); ctx.SaveChanges(); ClearTextField(); AddEditPlan_Load(null, null); MessageBox.Show("Plan SUccesfully Added"); } } } }
private void SaveEmployee() { if (dgvrAddEditEmployee == null) { try { using (AxaContext ctx = new AxaContext()) { var employee = new Employee { IdPicture = textBox1.Text, ELastName = tb_LastName.Text, EMiddleName = tb_MiddleName.Text, EFirstName = tb_FirstName.Text, BirthDate = dateTimePicker1.Value.Date, Age = tb_Age.Text, EmailAddress = tb_EmailAddress.Text, Gender = cb_Gender.SelectedItem.ToString(), CivilStatus = cb_CivilStatus.Text, CellphoneNo = tb_CellphoneNo.Text, Address = tb_Address.Text, DesignatedPostion = cb_DisignatedPosition.SelectedItem.ToString(), }; ctx.Employees.Add(employee); ctx.SaveChanges(); MessageBox.Show("Save Successful"); this.Close(); this.Dispose(); // Clearfields(); } } catch (System.Exception) { MessageBox.Show("Save unSuccessful"); } } else { try { int tempEployeeId = Int32.Parse(dgvrAddEditEmployee.Cells[0].Value.ToString()); using (AxaContext ctx = new AxaContext()) { var employeeUpdate = new Employee { EmployeeId = tempEployeeId, IdPicture = textBox1.Text, ELastName = tb_LastName.Text, EMiddleName = tb_MiddleName.Text, EFirstName = tb_FirstName.Text, BirthDate = dateTimePicker1.Value.Date, Age = tb_Age.Text, EmailAddress = tb_EmailAddress.Text, Gender = cb_Gender.SelectedItem.ToString(), CivilStatus = cb_CivilStatus.Text, CellphoneNo = tb_CellphoneNo.Text, Address = tb_Address.Text, DesignatedPostion = cb_DisignatedPosition.SelectedItem.ToString(), }; ctx.Employees.Attach(employeeUpdate); ctx.Entry(employeeUpdate).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); MessageBox.Show("Update Successful"); this.Close(); this.Dispose(); } } catch { } } }
private void SaveUser() { using (AxaContext ctx = new AxaContext()) { if (tb_FName.Text == "") { MessageBox.Show("Please Filled up all the fields"); } else { if (dataGridView_User.SelectedRows.Count > 0) { try { bool statusAccount = false; if (radioButton1.Checked == true) { statusAccount = true; } else { statusAccount = false; } int userId = Int32.Parse(dataGridView_User.SelectedRows[0].Cells[0].Value.ToString()); var userUpdate = new User { UserId = userId, UFirstName = tb_FName.Text, UMiddleName = tb_MName.Text, ULastName = tb_LName.Text, ContactNumber = tb_ContactNo.Text, UserName = tb_Username.Text, Password = tb_Password.Text, UAddress = tb_Address.Text, UGender = cb_Gender.GetItemText(cb_Gender.SelectedItem), Role = cb_Role.GetItemText(cb_Role.SelectedItem), IsActive = statusAccount }; ctx.Users.Attach(userUpdate); ctx.Entry(userUpdate).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); ClearTextField(); LoadUserList(); MessageBox.Show("Update Successful"); } catch (Exception) { } } else { var user = new User { UFirstName = tb_FName.Text, UMiddleName = tb_MName.Text, ULastName = tb_LName.Text, ContactNumber = tb_ContactNo.Text, UserName = tb_Username.Text, Password = tb_Password.Text, UAddress = tb_Address.Text, UGender = cb_Gender.GetItemText(cb_Gender.SelectedItem), Role = cb_Role.GetItemText(cb_Role.SelectedItem) }; ctx.Users.Add(user); ctx.SaveChanges(); ClearTextField(); LoadUserList(); MessageBox.Show("Save Successful"); } } } }