private void BtnAdd_Click(object sender, EventArgs e) { if (!Validation()) { return; } var customer = new Library.Models.Customer(); customer.FirstName = TxtFirstName.Text; customer.LastName = TxtLastName.Text; customer.PhoneNumber = TxtPhoneNo.Text; customer.Email = TxtEmail.Text; #region VERIFY THAT DUPLICATE EMAIL ADDRESS IS NOT ENTERED TO THE SYSTEM List <Library.Models.Customer> cs = _context.Customers.Where(c => c.Email.Contains(TxtEmail.Text)).ToList(); foreach (var item in cs) { if (item.Email == customer.Email) { LblEmail.ForeColor = Color.Red; MessageBox.Show("Email address has already been used, please use a different email.", "Attention!"); return; } } #endregion _context.Customers.Add(customer); _context.SaveChanges(); FillCustomerList(); ResetForm(); MessageBox.Show("New Customer is successfully added to the list.", "UPDATE"); }
private void DgvCustomer_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { BtnAdd.Visible = false; BtnUpdate.Visible = true; BtnDelete.Visible = true; foreach (var item in _context.Customers.ToList()) { int Id = Convert.ToInt32(DgvCustomer.Rows[e.RowIndex].Cells[0].Value.ToString()); SelectedCustomer = _context.Customers.Find(Id); TxtFirstName.Text = SelectedCustomer.FirstName; TxtLastName.Text = SelectedCustomer.LastName; TxtPhoneNo.Text = SelectedCustomer.PhoneNumber; TxtEmail.Text = SelectedCustomer.Email; } }