private void OK_Click(object sender, RoutedEventArgs e) { // Add the record to the datbase // Create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); Customer cust = con.Customers.Single(u => u.CustomerID == customer.CustomerID); // cust.CustomerID = ID.Text.Trim(); cust.CompanyName = CompanyName.Text.Trim(); cust.ContactName = ContactName.Text.Trim(); cust.ContactTitle = ContactTitle.Text.Trim(); cust.Country = Country.Text.Trim(); cust.City = City.Text.Trim(); cust.Region = Region.Text.Trim(); cust.PostalCode = PostalCode.Text.Trim(); cust.Address = Address.Text.Trim(); cust.Phone = Phone.Text.Trim(); cust.Fax = Fax.Text.Trim(); // submit changes record to the database con.SubmitChanges(); //show sucess message MessageBox.Show("Customer " + cust.CustomerID + " Was edited ok"); //update display grid to show changes Application app = Application.Current; MainWindow main = (MainWindow) app.MainWindow; main.refreshGrid(); DialogResult = true; }
}//end Cancel_Click //code to handle the Add button click event private void btnAdd_Click(object sender, RoutedEventArgs e) { //add the record to the database //the ID must be unique and specified or do not proceed with the add if (txtCustomerID.Text.Trim() == "") { MessageBox.Show("Missing unique Customer ID", "Entry error", MessageBoxButton.OK, MessageBoxImage.Error); return; } //add the record Customer cust = new Customer(); cust.CustomerID = txtCustomerID.Text.Trim(); cust.CompanyName = txtCompanyName.Text.Trim(); cust.ContactName = txtContactName.Text.Trim(); cust.ContactTitle = txtContactTitle.Text.Trim(); cust.Country = txtCountry.Text.Trim(); cust.City = txtCity.Text.Trim(); cust.Region = txtRegionState.Text.Trim(); cust.PostalCode = txtPostalCode.Text.Trim(); cust.Address = txtAddress.Text.Trim(); cust.Phone = txtPhone.Text.Trim(); cust.Fax = txtFax.Text.Trim(); //main try block try { //create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); //add new record to the database con.Customers.InsertOnSubmit(cust); con.SubmitChanges(); //tell the user this was added successfully MessageBox.Show("Customer " + cust.CustomerID + " was added successfully."); //refresh the data grid Application app = Application.Current; MainWindow main = (MainWindow)app.MainWindow; main.refreshGrid(); //exit the dialog window with a success result DialogResult = true; } catch (Exception ex) { //tell the user this was successful MessageBox.Show("The item '" + cust.CustomerID + "' could not be added. " + ex.ToString()); //exit the dialog window without doing anything. DialogResult = false; } }//end btnAdd_Click
}//end mnuitemEditRecord_Click //menu item click event for Delete Record private void mnuitmDeleteRecord_Click(object sender, RoutedEventArgs e) { //local variables string rowID; //create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); //figure out which row in the datagrid is selected object item = CustomerDataGrid.SelectedItem; //try block to prevent an error found during coding. The delete method throws an error if no row //is selected in the datagrid. try { rowID = (CustomerDataGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text; } catch { //no rows are highlighted in the grid MessageBox.Show("Please select a row in the datagrid to delete and try again."); return; } //check to make sure we received a valid rowID if (rowID != null) { //main try/catch block try { //get the target row var cust = (from c in con.Customers where c.CustomerID == rowID select c).First(); //delete this record from the database con.Customers.DeleteOnSubmit(cust); con.SubmitChanges(); //refresh the datagrid refreshGrid(); //tell the user this was successful MessageBox.Show("The item '" + rowID + "' was deleted successfully."); } catch (Exception ex) { //tell the user this was successful MessageBox.Show("The item '" + rowID + "' failed to delete. " + ex.ToString()); } } }//end mnuitmDeleteRecord_Click
}//end Cancel_Click //code to handle the Save button click event private void btnSave_Click(object sender, RoutedEventArgs e) { //main try/catch block try { //create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); // Query the database for the row to be updated. var cust = con.Customers.SingleOrDefault(c => c.CustomerID == rowID); //set all of the field values the record cust.CustomerID = rowID; cust.CompanyName = txtCompanyName.Text.Trim(); cust.ContactName = txtContactName.Text.Trim(); cust.ContactTitle = txtContactTitle.Text.Trim(); cust.Country = txtCountry.Text.Trim(); cust.City = txtCity.Text.Trim(); cust.Region = txtRegionState.Text.Trim(); cust.PostalCode = txtPostalCode.Text.Trim(); cust.Address = txtAddress.Text.Trim(); cust.Phone = txtPhone.Text.Trim(); cust.Fax = txtFax.Text.Trim(); //save record to the database con.SubmitChanges(); //tell the user this was updated successfully MessageBox.Show("Customer '" + rowID + "' was successfully updated."); //refresh the data grid Application app = Application.Current; MainWindow main = (MainWindow)app.MainWindow; main.refreshGrid(); //exit the dialog window with a success result DialogResult = true; } catch (Exception ex) { //tell the user this was successful MessageBox.Show("The item '" + rowID + "' could not be saved. " + ex.ToString()); //exit the dialog window without doing anything. DialogResult = false; } }//end btnSave_Click
private void OK_Click(object sender, RoutedEventArgs e) { // Add the record to the datbase // The ID must be unique and specified or do not proceed with the add if (ID.Text.Trim() == "") { MessageBox.Show("Missing unique customer ID", "Entry error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Add the record Customer cust = new Customer(); cust.CustomerID = ID.Text.Trim(); cust.CompanyName = CompanyName.Text.Trim(); cust.ContactName = ContactName.Text.Trim(); cust.ContactTitle = ContactTitle.Text.Trim(); cust.Country = Country.Text.Trim(); cust.City = City.Text.Trim(); cust.Region = Region.Text.Trim(); cust.PostalCode = PostalCode.Text.Trim(); cust.Address = Address.Text.Trim(); cust.Phone = Phone.Text.Trim(); cust.Fax = Fax.Text.Trim(); // Create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); // Add new record to the database con.Customers.InsertOnSubmit(cust); con.SubmitChanges(); MessageBox.Show("Customer " + cust.CustomerID + " Was added ok"); //refresh the display grid to show changes Application app = Application.Current; MainWindow main = (MainWindow) app.MainWindow; main.refreshGrid(); DialogResult = true; }
private void Delete_Click(object sender, RoutedEventArgs e) { /** Customer remove = new Customer(); String CustID = DeleteID.Text.Trim(); // Create an instance of a data connection to the database customer table CustomerDataDataContext con = new CustomerDataDataContext(); remove = (from cust in con.Customers where (cust.CustomerID == CustID) // select cust).SingleOrDefault(); select cust).FirstOrDefault(); if (object.Equals(remove, default(Customer))) // if (remove == null) { MessageBox.Show("Customer Does not Exist!"); } else { // delete record from the database con.Customers.DeleteOnSubmit(remove); con.SubmitChanges(); MessageBox.Show("Customer " + CustID + " Was Deleted ok"); } Application app = Application.Current; MainWindow main = (MainWindow)app.MainWindow; main.refreshGrid(); DialogResult = true; **/ String CustID = DeleteID.Text.Trim(); try { using (CustomerDataDataContext con = new CustomerDataDataContext()) { //get customer that match matches ID from customer table var q = (from c in con.GetTable<Customer>() where c.CustomerID == CustID select c).Single<Customer>(); //get all orders from Order that include customer ID foreach (Order ord in q.Orders) { //loop through order and mark for deletion records with matching customer con.GetTable<Order>().DeleteOnSubmit(ord); //loop through order details and mark for deletion records with matching order foreach (Order_Detail od in ord.Order_Details) { con.GetTable<Order_Detail>().DeleteOnSubmit(od); } } //delete matching customer entry con.GetTable<Customer>().DeleteOnSubmit(q); con.SubmitChanges(); MessageBox.Show("Customer " + CustID + " Was Deleted ok"); } //refresh display grid to show changes Application app = Application.Current; MainWindow main = (MainWindow)app.MainWindow; main.refreshGrid(); DialogResult = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }