private void btnAddFacilitatorAddress_Click(object sender, EventArgs e) { using (frmAddUpdateAddresses frm = new frmAddUpdateAddresses(0)) { frm.ShowDialog(); if (frm.CurrentAddress != null) { if (frm.CurrentAddress.AddressID != 0) { using (var Dbconnection = new MCDEntities()) { Data.Models.Facilitator CurrentFacilitator = ((Data.Models.Facilitator)facilitatorBindingSource.Current); Dbconnection.Facilitators.Attach(CurrentFacilitator); Dbconnection.Addresses.Attach(frm.CurrentAddress); CurrentFacilitator.Individual.Addresses.Add(frm.CurrentAddress); Dbconnection.SaveChanges(); //Dbconnection.Entry(CurrentFacilitator).Reference(a => a.Individual.Addresses).Load(); // Dbconnection.Entry(CurrentFacilitator).Reference(a => a.LookupProvince).Load(); //// Dbconnection.Entry(CurrentFacilitator).Reference(a => a.LookupCountry).Load(); }; refreshFacilitatorAddressDetails(); } } }; }
private void btnAddStudentAddress_Click(object sender, EventArgs e) { frmAddUpdateAddresses frm = new frmAddUpdateAddresses(0); frm.ShowDialog(); if (frm.CurrentAddress != null) { if (frm.CurrentAddress.AddressID != 0) { using (var Dbconnection = new MCDEntities()) { Data.Models.Company CurrentCompany = ((Data.Models.Company)companyBindingSource.Current); Dbconnection.Companies.Attach(CurrentCompany); Dbconnection.Addresses.Attach(frm.CurrentAddress); CurrentCompany.Addresses.Add(frm.CurrentAddress); Dbconnection.SaveChanges(); Dbconnection.Entry(frm.CurrentAddress).Reference("LookupAddressType").Load(); Dbconnection.Entry(frm.CurrentAddress).Reference("LookupProvince").Load(); Dbconnection.Entry(frm.CurrentAddress).Reference("LookupCountry").Load(); }; refreshCompanyAddresses(); } } }
private void btnUpdateAddress_Click(object sender, EventArgs e) { if (companyAddressesBindingSource.Count > 0) { CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current); Address CurrentAddress = (Address)CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentCompanyAddress.AddressID).FirstOrDefault(); using (frmAddUpdateAddresses frm = new frmAddUpdateAddresses(CurrentCompanyAddress.AddressID, CurrentAddress)) { frm.ShowDialog(); if (frm.CurrentAddress.AddressIsDefault) { resetAdressDefaults(CurrentAddress.AddressID); } else if (CurrentlySelectedCompany.Addresses.Count == 1) { using (var Dbconnection = new MCDEntities()) { CurrentAddress.AddressIsDefault = true; Address Add = Dbconnection.Addresses.Where(a => a.AddressID == CurrentAddress.AddressID).FirstOrDefault(); Add.AddressIsDefault = true; //Dbconnection.Entry(Add).State = EntityState.Modified; Dbconnection.SaveChanges(); }; } refreshCompanyAddresses(); } } }
private void btnAddEmployeeAddress_Click(object sender, EventArgs e) { frmAddUpdateAddresses frm = new frmAddUpdateAddresses(); Address NewAddress = new Address { AddressID = 0 }; frm.CurrentAddress = NewAddress; frm.ShowDialog(); if (frm.CurrentAddress.AddressID != 0) { using (var Dbconnection = new MCDEntities()) { Employee CurrentEmployee = ((Employee)employeeBindingSource.Current); Dbconnection.Employees.Attach(CurrentEmployee); Dbconnection.Addresses.Attach(frm.CurrentAddress); CurrentEmployee.Individual.Addresses.Add(frm.CurrentAddress); Dbconnection.SaveChanges(); //Dbconnection.Entry(CurrentEmployee).Reference(a => a.Individual.Addresses).Load(); // Dbconnection.Entry(CurrentEmployee).Reference(a => a.LookupProvince).Load(); //// Dbconnection.Entry(CurrentEmployee).Reference(a => a.LookupCountry).Load(); }; refreshEmployeeAddressDetails(); } }
private void btnUpdateAddress_Click(object sender, EventArgs e) { Address CompanyAddressToBeUpdated = (Address)addressBindingSource.Current; frmAddUpdateAddresses frm = new frmAddUpdateAddresses(CompanyAddressToBeUpdated.AddressID); frm.CurrentAddress = CompanyAddressToBeUpdated; frm.ShowDialog(); refreshCompanyAddresses(); }
private void btnUpdateAddress_Click(object sender, EventArgs e) { Address FacilitatorAddressToBeUpdated = (Address)addressesBindingSource.Current; frmAddUpdateAddresses frm = new frmAddUpdateAddresses(FacilitatorAddressToBeUpdated.AddressID); frm.CurrentAddress = FacilitatorAddressToBeUpdated; frm.ShowDialog(); refreshFacilitatorAddressDetails(); }
private void btnUpdateAddress_Click(object sender, EventArgs e) { Address EmployeeAddressToBeUpdated = (Address)addressesBindingSource.Current; frmAddUpdateAddresses frm = new frmAddUpdateAddresses(); frm.AddressID = EmployeeAddressToBeUpdated.AddressID; frm.CurrentAddress = EmployeeAddressToBeUpdated; frm.ShowDialog(); refreshEmployeeAddressDetails(); }
private void btnAddEmployeeAddress_Click(object sender, EventArgs e) { frmAddUpdateAddresses frm = new frmAddUpdateAddresses(0); frm.ShowDialog(); if (frm.CurrentAddress.AddressID != 0) { using (var Dbconnection = new MCDEntities()) { Employee CurrentEmployee = ((Employee)employeeBindingSource.Current); Dbconnection.Employees.Attach(CurrentEmployee); Dbconnection.Addresses.Attach(frm.CurrentAddress); CurrentEmployee.Individual.Addresses.Add(frm.CurrentAddress); Dbconnection.SaveChanges(); }; refreshEmployeeAddressDetails(); } }
private void btnRemoveStudentAddress_Click(object sender, EventArgs e) { Boolean bWasDefault = false; if (companyAddressesBindingSource.Count > 0) { CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current); Address CurrentAddress = (Address)CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentCompanyAddress.AddressID).FirstOrDefault(); if (CurrentAddress.AddressIsDefault) { bWasDefault = true; } using (frmAddUpdateAddresses frm = new frmAddUpdateAddresses(CurrentCompanyAddress.AddressID, CurrentAddress)) { using (var Dbconnection = new MCDEntities()) { using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction()) { try { //CRUD Operations // return one instance each entity by primary key var company = Dbconnection.Companies.FirstOrDefault(p => p.CompanyID == CurrentlySelectedCompany.CompanyID); var AddressToRemove = Dbconnection.Addresses.FirstOrDefault(s => s.AddressID == CurrentCompanyAddress.AddressID); // call Remove method from navigation property for any instance // supplier.Product.Remove(product); // also works company.Addresses.Remove(AddressToRemove); ////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(); } } }; CurrentlySelectedCompany.Addresses.Remove(CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentAddress.AddressID).First()); if (CurrentlySelectedCompany.Addresses.Count > 0) { if (bWasDefault) { var CurrentlySelectedToUpdate = CurrentlySelectedCompany.Addresses.First(); using (var Dbconnection = new MCDEntities()) { var x = Dbconnection.Addresses.Where(a => a.AddressID == CurrentlySelectedToUpdate.AddressID).First(); x.AddressIsDefault = true; Dbconnection.Entry(x).State = EntityState.Modified; Dbconnection.SaveChanges(); }; CurrentlySelectedToUpdate.AddressIsDefault = true; } } refreshCompanyAddresses(); } } }
private void btnAddCompanyAddress_Click(object sender, EventArgs e) { frmAddUpdateAddresses frm = new frmAddUpdateAddresses(0); frm.ShowDialog(); if (frm.CurrentAddress != null) { if (frm.CurrentAddress.AddressID != 0) { if (frm.CurrentAddress.AddressIsDefault) { resetAdressDefaults(); } else if (CurrentlySelectedCompany.Addresses.Count == 0) { frm.CurrentAddress.AddressIsDefault = true; } using (var Dbconnection = new MCDEntities()) { using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction()) { try { //CRUD Operations CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current); /* * this steps follow to both entities * * 1 - create instance of entity with relative primary key * * 2 - add instance to context * * 3 - attach instance to context */ // 1 Data.Models.Company C = new Data.Models.Company { CompanyID = CurrentlySelectedCompany.CompanyID }; // 2 Dbconnection.Companies.Add(C); // 3 Dbconnection.Companies.Attach(C); // 1 Address A = new Address { AddressID = frm.CurrentAddress.AddressID }; // 2 Dbconnection.Addresses.Add(A); // 3 Dbconnection.Addresses.Attach(A); // like previous method add instance to navigation property C.Addresses.Add(A); ////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(); } } }; using (var Dbconnection = new MCDEntities()) { Dbconnection.Addresses.Attach(frm.CurrentAddress); Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupAddressType).Load(); Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupProvince).Load(); Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupCountry).Load(); CurrentlySelectedCompany.Addresses.Add(frm.CurrentAddress); }; refreshCompanyAddresses(); } } }