Ejemplo n.º 1
0
        /// <summary>
        /// Register
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_register_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // gender
                string gender = (bool)rb_male.IsChecked ? "Male" : "Female";

                // customer object
                Customer customer = new Customer()
                {
                    FirstName              = tb_firstName.Text,
                    LastName               = tb_lastName.Text,
                    Address                = tb_address.Text,
                    Phone                  = tb_phoneNumber.Text,
                    BirthDate              = dp_birth.Text,
                    Gender                 = gender,
                    EnmergencyName         = tb_enName.Text,
                    EnmergencyRelationship = tb_enRelation.Text,
                    EnmergencyPhone        = tb_enPhone.Text
                };

                // Register as new customer
                if (editCustomer == null)
                {
                    // create new database insert
                    Database        db          = new Database();
                    Insert          newCustomer = new Insert();
                    MySqlConnection conn        = db.CreateConnection();
                    bool            status      = false;
                    // open connection
                    try
                    {
                        // customer object, it uses SETTER which will throw exception if empty

                        conn.Open();

                        status = newCustomer.AddCustomer(db.CreateCommand(conn), customer);

                        conn.Close();
                        if (status)
                        {
                            MessageBox.Show("Customer Added");
                            clear();
                        }
                        else
                        {
                            MessageBox.Show("Fail to add customer");
                        }
                    }
                    catch (CustomerException c)
                    {
                        MessageBox.Show("Required: " + c.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Database: " + ex.Message.ToString());
                    }

                    // Edit the current customer in database
                }
                else
                {
                    // call update on database
                    Database        db     = new Database();
                    Update          update = new Update();
                    MySqlConnection conn   = db.CreateConnection();
                    conn.Open();

                    // set the uid for editing
                    customer.Uid = editCustomer.Uid;

                    if (update.EditCustomer(db.CreateCommand(conn), customer))
                    {
                        MessageBox.Show("Customer Edited");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Fail to update information.");
                    }
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Edit error: " + ex.Message.ToString());
            }
        }