private void createNewCustomer() { using (var ctx = new DataLayer.ScheduleEntities()) { DataLayer.customer newCust = new DataLayer.customer(); newCust.customerName = textBoxName.Text; newCust.active = checkBoxActive.Checked; newCust.addressId = createNewAddress(); newCust.lastUpdate = DateTime.Now.ToUniversalTime(); newCust.lastUpdateBy = CurrentUserName; newCust.createdBy = CurrentUserName; newCust.createDate = DateTime.Now.ToUniversalTime(); customerBindingSource.EndEdit(); // try to save changes try { ctx.customers.Add(newCust); ctx.SaveChanges(); // write changes to database file this.Close(); DialogResult = DialogResult.OK; } catch (DbEntityValidationException) { MessageBox.Show("Something has gone wrong saving this data", "Entity Validation Exception"); } catch (Exception e) { MessageBox.Show("Unable to update the db", e.Message); } } }
private void updateExistingCustomer(DataLayer.customer cust) { using (var ctx = new DataLayer.ScheduleEntities()) { var customerUpdate = (from cs in ctx.customers join a in ctx.addresses on cs.addressId equals a.addressId join c in ctx.cities on a.cityId equals c.cityId join cn in ctx.countries on c.countryId equals cn.countryId where cs.customerId == cust.customerId //orderby appt.start select new { cs, a, c, cn }).Single(); //`customerId`, `customerName`, `addressId`, `active`, `createDate`, `createdBy`, `lastUpdate`, `lastUpdateBy` customerUpdate.cs.customerName = textBoxName.Text; customerUpdate.a.address1 = textBoxAddress1.Text; customerUpdate.a.address2 = textBoxAddress2.Text; customerUpdate.a.postalCode = textBoxPostCode.Text; customerUpdate.a.cityId = comboBoxCity.SelectedIndex + 1; customerUpdate.cs.active = checkBoxActive.Checked; customerUpdate.a.phone = textBoxPhone.Text; customerUpdate.cs.lastUpdate = DateTime.Now.ToUniversalTime(); customerUpdate.cs.lastUpdateBy = CurrentUserName; if (customerUpdate.cs.createdBy is null) { customerUpdate.cs.createdBy = CurrentUserName; } Validate(); // validate the input fields //appointmentBindingSource.EndEdit(); // try to save changes try { ctx.SaveChanges(); // write changes to database file this.Close(); DialogResult = DialogResult.OK; } catch (DbEntityValidationException) { MessageBox.Show("Something has gone wrong saving this data", "Entity Validation Exception"); } } }
public MakeCustomer(MainScreen mainForm, int custId) : this(mainForm) { try { this.cust = dbcontext.customers .Where(c => c.customerId == custId) .FirstOrDefault(); } catch (NullReferenceException) { MessageBox.Show("An appointment to modify could not be loaded"); this.Hide(); } using (var ctx = new DataLayer.ScheduleEntities()) { var addressJoin = (from a in dbcontext.addresses join c in dbcontext.cities on a.cityId equals c.cityId join cn in dbcontext.countries on c.countryId equals cn.countryId where a.addressId == cust.addressId //orderby appt.start select new { a.addressId, a.address1, a.address2, a.phone, postCode = a.postalCode, cityIndex = c.cityId, countryIndex = cn.countryId }).ToList().First(); //`customerId`, `customerName`, `addressId`, `active`, `createDate`, `createdBy`, `lastUpdate`, `lastUpdateBy` textBoxName.Text = cust.customerName; textBoxAddress1.Text = addressJoin.address1; textBoxAddress2.Text = addressJoin.address2; textBoxPostCode.Text = addressJoin.postCode; comboBoxCity.SelectedIndex = addressJoin.cityIndex - 1; comboBoxCountry.SelectedIndex = addressJoin.countryIndex - 1; textBoxPhone.Text = addressJoin.phone; addr.addressId = addressJoin.addressId; checkBoxActive.Checked = cust.active; } }