private void btnSave_Click(object sender, EventArgs e) { try { using (mCoyoteDBEntities1 context = new mCoyoteDBEntities1()) { Customer customer = new Customer() { custFirstName = txtFirstName.Text, custLastName = txtLastName.Text, custAddress = txtAddress.Text, custEmail = txtEmail.Text, custPhone = txtPhone.Text }; context.Customers.Add(customer); context.SaveChanges(); this.Close(); } } catch (Exception) { MessageBox.Show("You must enter a value for each item"); } }
private void btnSearchCustomer_Click(object sender, EventArgs e) { if (txtSearchCustomer.Text != "") { using (mCoyoteDBEntities1 context = new mCoyoteDBEntities1()) { Customer customer = new Customer(); string userSelected = txtSearchCustomer.Text; if (cbCustSearchOptions.Text == "First Name") { var data = from b in context.Customers where b.custFirstName.Contains(userSelected) select b; dtgCustomers.DataSource = data.ToList(); } else if (cbCustSearchOptions.Text == "Last Name") { var data = from b in context.Customers where b.custLastName.Contains(userSelected) select b; dtgCustomers.DataSource = data.ToList(); } else if (cbCustSearchOptions.Text == "Address") { var data = from b in context.Customers where b.custAddress.Contains(userSelected) select b; dtgCustomers.DataSource = data.ToList(); } else if (cbCustSearchOptions.Text == "Email") { var data = from b in context.Customers where b.custEmail.Contains(userSelected) select b; dtgCustomers.DataSource = data.ToList(); } else if (cbCustSearchOptions.Text == "Phone") { var data = from b in context.Customers where b.custPhone.Contains(userSelected) select b; dtgCustomers.DataSource = data.ToList(); } else { MessageBox.Show("Please select a search criteria"); } } } }
private void btnEditCutomer_Click(object sender, EventArgs e) { using (mCoyoteDBEntities1 context = new mCoyoteDBEntities1()) { Customer customer = new Customer(); foreach (DataGridViewRow row in this.dtgCustomers.SelectedRows) { Customer cust = row.DataBoundItem as Customer; if (cust != null) { frmEditCustomer editCust = new frmEditCustomer(cust.Id); editCust.ShowDialog(); } } loadCustomerDatagrid(); } }
private void btnDeleteCustomer_Click(object sender, EventArgs e) { DialogResult dialog = MessageBox.Show("Are you sure you want to delete this customer?", "Delete Customer", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialog == DialogResult.Yes) { using (mCoyoteDBEntities1 context = new mCoyoteDBEntities1()) { Customer customer = new Customer(); foreach (DataGridViewRow row in this.dtgCustomers.SelectedRows) { Customer cust = row.DataBoundItem as Customer; if (cust != null) { Customer cust_to_delete = context.Customers.Single(c => c.Id == cust.Id); context.Customers.Remove(cust_to_delete); } } context.SaveChanges(); loadCustomerDatagrid(); } } }