コード例 #1
0
        private void GetCustomer(int CustomerID)
        {
            try
            {
                // Code a query to retrieve the selected customer
                // and store the Customer object in the class variable.
                // Note to self: LINQ query is bottom-up.
                selectedCustomer = entities.Customers
                                   .Where(cust => cust.CustomerID == CustomerID)
                                   .Select(cust => cust).Single();

                if (selectedCustomer == null)
                {
                    MessageBox.Show("No customer found with this ID. " +
                                    "Please try again.", "Customer Not Found");
                    this.ClearControls();
                    txtCustomerID.Focus();
                }
                else
                {
                    //  If the customer is found, add code to the GetCustomer method that checks if the State object
                    // has been loaded and that loads if it hasn't.
                    if (!entities.Entry(selectedCustomer).Reference("State").IsLoaded)
                    {
                        entities.Entry(selectedCustomer).Reference("State").Load();
                    }
                    this.DisplayCustomer();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
コード例 #2
0
        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (IsValidData())
            {
                if (addCustomer)
                {
                    customer = new Customer();
                    this.PutCustomerData(customer);

                    // Add the new vendor to the collection of vendors.
                    encontext.Customers.Add(customer);
                    encontext.SaveChanges();

                    try
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
                else
                {
                    this.PutCustomerData(customer);
                    try
                    {
                        // Update the database.
                        encontext.SaveChanges();
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        ex.Entries.Single().Reload();
                        if (encontext.Entry(customer).State
                            == EntityState.Detached)
                        {
                            MessageBox.Show("Another user has deleted that customer.",
                                            "Concurrency Error");
                            this.DialogResult = DialogResult.Abort;
                        }
                        else
                        {
                            MessageBox.Show("Another user has updated that customer.",
                                            "Concurrency Error");
                            this.DialogResult = DialogResult.Retry;
                        }
                    }
                    // Add concurrency error handling.
                    // Place the catch block before the one for a generic exception.

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
            }
        }