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;
        }
Ejemplo n.º 2
0
        }//end btnSave_Click

        //pull the data from the database and place the initial values in each control on this form
        private void EditRecord_Loaded(object sender, RoutedEventArgs e)
        {
            //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);

            //the query should have returned 1 row
            if (cust != null)
            {
                //set all of the field values the record
                txtCustomerID.Text   = cust.CustomerID;
                txtCompanyName.Text  = cust.CompanyName;
                txtContactName.Text  = cust.ContactName;
                txtContactTitle.Text = cust.ContactTitle;
                txtCountry.Text      = cust.Country;
                txtCity.Text         = cust.City;
                txtRegionState.Text  = cust.Region;
                txtPostalCode.Text   = cust.PostalCode;
                txtAddress.Text      = cust.Address;
                txtPhone.Text        = cust.Phone;
                txtFax.Text          = cust.Fax;

                //put the cursor at the end of the company name textbox
                txtCompanyName.Focus();
                txtCompanyName.SelectionStart = txtCompanyName.Text.Length;
            }
        }//end EditRecord_Loaded
        private void FindRecord_Click(object sender, RoutedEventArgs e)
        {
            //create a new empty customer object
            Customer find = new Customer();

            //get customer ID from form
            String CustID = FindID.Text.Trim();

            // Create an instance of a data connection to the database customer table
            CustomerDataDataContext con = new CustomerDataDataContext();

            //find customer based upod ID
            find = (from cust in con.Customers
                      where (cust.CustomerID == CustID)
                      select cust).FirstOrDefault();

            //if a matching customer is not found
            if (object.Equals(find, default(Customer)))

            {
                MessageBox.Show("Customer Does not Exist!");
            }
            //if found proceed
            else
            {
                // Close the find edit window
                DialogResult = false;

                //open the edit record window
                EditRecord editRecordWindow = new EditRecord(find);
                editRecordWindow.ShowDialog();

            }
        }
        }//end Grid_Loaded

        //function to refresh the data in the grid
        //this is not only called here, but from the Add and Edit Record forms 
        //and from the Delete Record method 
        public void refreshGrid()
        {
            //create an instance of a data connection to the database customer table
            CustomerDataDataContext con = new CustomerDataDataContext();

            //create a customer list
            List<Customer> customers = (from c in con.Customers select c).ToList();
            CustomerDataGrid.ItemsSource = customers;
        }//end refreshGrid
        }//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 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 Grid_Loaded

        //function to refresh the data in the grid
        //this is not only called here, but from the Add and Edit Record forms
        //and from the Delete Record method
        public void refreshGrid()
        {
            //create an instance of a data connection to the database customer table
            CustomerDataDataContext con = new CustomerDataDataContext();

            //create a customer list
            List <Customer> customers = (from c in con.Customers select c).ToList();

            CustomerDataGrid.ItemsSource = customers;
        }//end refreshGrid
        }//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
Ejemplo n.º 10
0
        }//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;
        }
        }//end btnSave_Click

        //pull the data from the database and place the initial values in each control on this form
        private void EditRecord_Loaded(object sender, RoutedEventArgs e)
        {
            //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);

            //the query should have returned 1 row
            if(cust != null)
            {
                //set all of the field values the record
                txtCustomerID.Text = cust.CustomerID;
                txtCompanyName.Text = cust.CompanyName;
                txtContactName.Text = cust.ContactName;
                txtContactTitle.Text = cust.ContactTitle;
                txtCountry.Text = cust.Country;
                txtCity.Text = cust.City;
                txtRegionState.Text = cust.Region;
                txtPostalCode.Text = cust.PostalCode;
                txtAddress.Text = cust.Address;
                txtPhone.Text = cust.Phone;
                txtFax.Text = cust.Fax;

                //put the cursor at the end of the company name textbox
                txtCompanyName.Focus();
                txtCompanyName.SelectionStart = txtCompanyName.Text.Length;
            }
        }//end EditRecord_Loaded
        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);
            }
        }
        }//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
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            // Create an instance of a data connection to the database customer
            // table
            CustomerDataDataContext con = new CustomerDataDataContext();

            // Create a customer list
            List<Customer> customers = (from c in con.Customers
                                        select c).ToList();
            CustomerDataGrid.ItemsSource = customers;
        }