コード例 #1
0
        /// <summary>
        /// Attempts to Update the object and Database when Update button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Update_Click(object sender, EventArgs e)
        {
            string successMessage = "";

            using (var conn = new MySqlConnection(connecterString))
            {
                try
                {
                    //Update information with Information with Textboxes, the user and dates updated
                    if (selectedCustomer != 0)
                    {
                        myCustomer = new Customer(selectedCustomer, conn);
                        // If the Address is changing, create a new address, and link the new address to the customer
                        string textBoxCheckAddress  = Txt_Address1.Text + Txt_Address2.Text + Txt_City.Text;
                        string databaseCheckAddress = myCustomer.addressObject.address + myCustomer.addressObject.address2 + myCustomer.addressObject.cityObject.cityName;
                        int    updatingAddressId    = (int)myCustomer.addressID;

                        //Set the values to the customer Object based on the
                        myCustomer.customerName = Txt_CustName.Text;
                        myCustomer.lastUpdate   = DateTime.Now;
                        myCustomer.lastUpdateBy = loggedInUser;


                        if (textBoxCheckAddress != databaseCheckAddress)
                        {
                            Address updatedAddress = new Address(Txt_Address1.Text, Txt_Address2.Text, Txt_City.Text, Txt_Country.Text, Txt_PostalCode.Text, Txt_PhoneNumber.Text, loggedInUser, conn);
                            myCustomer.addressID = updatedAddress.addressId;
                            updatingAddressId    = myCustomer.addressID;
                        }



                        string myUpdateCustomersQuery = "UPDATE customer c JOIN address a ON c.addressId = a.addressId "
                                                        + "SET c.customerName = '" + Txt_CustName.Text.ToString() + "', c.addressId = "
                                                        + updatingAddressId.ToString() + ", c.lastUpdate = '" + DateTime.Now.ToString("yyyy-MM-dd")
                                                        + "', c.lastUpdateBy = '" + loggedInUser + "', a.phone = '" + Txt_PhoneNumber.Text
                                                        + "', a.lastUpdate = '" + DateTime.Now.ToString("yyyy-MM-dd")
                                                        + "', a.lastUpdateBy = '" + loggedInUser + "' "
                                                        + "WHERE c.customerId = " + myCustomer.customerID.ToString();
                        //DataChanges dt = new DataChanges();
                        DataChanges.ExecuteMySqlCommand(myUpdateCustomersQuery, conn);
                        customerAdded  = true;
                        successMessage = "Customer Information successfully Updated";
                    }
                    else
                    {
                        //int id = int.Parse(Txt_CustId.Text);
                        myCustomer = new Customer(Txt_CustName.Text, Txt_Address1.Text, Txt_Address2.Text, Txt_City.Text, Txt_Country.Text, Txt_PostalCode.Text, Txt_PhoneNumber.Text, loggedInUser, conn);
                        Address myAddress = myCustomer.addressObject;
                        Txt_CustId.Text      = myCustomer.customerID.ToString();
                        Txt_CustName.Text    = myCustomer.customerName;
                        Txt_Address1.Text    = myAddress.address;
                        Txt_Address2.Text    = myAddress.address2;
                        Txt_City.Text        = myAddress.cityObject.cityName;
                        Txt_Country.Text     = myAddress.cityObject.countryObject.country;
                        Txt_PhoneNumber.Text = myAddress.phone;
                        Txt_PostalCode.Text  = myAddress.postalCode;
                        successMessage       = "Customer Successfully Added";
                    }
                    MessageBox.Show(successMessage);

                    //If customer is created, set customerAdded = true to use if creation is cancelled
                    customerAdded = true;
                    Close();
                }
                catch (Exception innerException)
                {
                    MessageBox.Show("Add/Update Failed \n", innerException.ToString());
                }
            }
        }