Beispiel #1
0
        /// <summary>
        /// Clicking button checks if all fields have plausible entires (not empty or wrong format). Then
        /// updates currently selected customer with new information. Uses email as unique identifier, but
        /// does not check if email already exists. (Not sure how...)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveButton_Click(object sender, EventArgs e)
        {
            bool valid = true; // Boolean will be set to false if any tests fail, used to check if customer should be saved.

            if (FirstNameBox.Text == "" || !Regex.IsMatch(FirstNameBox.Text, name))
            {
                MessageBox.Show("Please enter your first name", "First name is a required field");
                FirstNameBox.Focus();
                valid = false;
            }
            if (LastNameBox.Text == "" || !Regex.IsMatch(LastNameBox.Text, name))
            {
                MessageBox.Show("Please enter your last name", "Last name is a required field");
                LastNameBox.Focus();
                valid = false;
            }
            if (AddressBox.Text == "" || !Regex.IsMatch(AddressBox.Text, address))
            {
                MessageBox.Show("Please enter your address", "Address is a required field");
                AddressBox.Focus();
                valid = false;
            }
            if (CityBox.Text == "" || !Regex.IsMatch(CityBox.Text, city))
            {
                MessageBox.Show("Please enter your city", "City is a required field");
                CityBox.Focus();
                valid = false;
            }
            if (StateBox.Text == "" || !Regex.IsMatch(StateBox.Text, name))
            {
                MessageBox.Show("Please enter your state", "State is a required field");
                StateBox.Focus();
                valid = false;
            }
            if (ZipBox.Text == "" || !Regex.IsMatch(ZipBox.Text, zip))
            {
                MessageBox.Show("Please enter your zip code", "Zip Code is a required field");
                ZipBox.Focus();
                valid = false;
            }
            if (PhoneBox.Text == "" || !Regex.IsMatch(PhoneBox.Text, phone))
            {
                MessageBox.Show("Please enter your phone number", "Phone number is a required field");
                PhoneBox.Focus();
                valid = false;
            }
            if (EmailBox.Text == "" || !Regex.IsMatch(EmailBox.Text, email))
            {
                MessageBox.Show("Please enter your email address", "Email is a required field");
                EmailBox.Focus();
                valid = false;
            }

            // If invalid entry, do not save.
            if (!valid)
            {
                return;
            }

            // Else
            string          ConnectionString = "server=localhost;user=root;database=book store;password="******"Insert into customer values" +
                                  $"('null','{FirstNameBox.Text}'," +
                                  $"'{LastNameBox.Text}'," +
                                  $"'{AddressBox.Text}'," +
                                  $"'{CityBox.Text}'," +
                                  $"'{StateBox.Text}'," +
                                  $"'{ZipBox.Text}'," +
                                  $"'{PhoneBox.Text}'," +
                                  $"'{EmailBox.Text}')";

                cmd.Connection = DBConnect;
                cmd.ExecuteNonQuery();
                DBConnect.Close();

                FirstNameBox.Clear();
                LastNameBox.Clear();
                AddressBox.Clear();
                CityBox.Clear();
                StateBox.Clear();
                ZipBox.Clear();
                PhoneBox.Clear();
                EmailBox.Clear();

                // Call function to update the combobox with new customer added.
                CustomerSelectBox_Click(sender, e);

                CustomerSelectBox.Enabled = true;
                SaveButton.Enabled        = false;
                MessageBox.Show("Customer added to database.");
                return;
            }
            // Else update the currently selected customer with textbox info

            // Extract first and last name into string array.
            string[] custName = new string[2];
            custName = CustomerSelectBox.Text.Split();

            cmd.CommandText = $"Update customer set" +
                              $" first='{FirstNameBox.Text}'," +
                              $"last='{LastNameBox.Text}'," +
                              $"address='{AddressBox.Text}'," +
                              $"city='{CityBox.Text}'," +
                              $"state='{StateBox.Text}'," +
                              $"zip='{ZipBox.Text}'," +
                              $"phone='{PhoneBox.Text}'," +
                              $"email='{EmailBox.Text}'" +
                              $" where first='{custName[0]}' AND last ='{custName[1]}'";
            cmd.Connection = DBConnect;
            cmd.ExecuteNonQuery();
            DBConnect.Close();
            MessageBox.Show("Customer successfully updated.");
            return;



            /*
             * SaveButton.Enabled = false;     // Disable the save button again until new customer is selected from the comboBox.
             *
             * // Rewrite the entire file, omitting the line with the email of the customer that is being updated, since email is unique.
             * // SRC: https://stackoverflow.com/questions/10371630/c-sharp-text-file-search-for-specific-word-and-delete-whole-line-of-text-that-co
             * Customer tempCustomer = (Customer)(CustomerSelectBox.SelectedItem);
             * var oldLines = File.ReadAllLines("customers.txt");
             * var newLines = oldLines.Where(line => !line.Contains(tempCustomer.email));
             * File.WriteAllLines("customers.txt", newLines);
             *
             * customers.Remove((Customer)CustomerSelectBox.SelectedItem);  // Remove old customer info from customer list.
             *
             * // Add new info to list.
             * tempCustomer = new Customer(FirstNameBox.Text, LastNameBox.Text, AddressBox.Text, CityBox.Text, StateBox.Text, ZipBox.Text, PhoneBox.Text, EmailBox.Text);
             * customers.Add(tempCustomer);
             *
             * // Close reader and inFile even if exception is thrown.
             * // SRC:https://stackoverflow.com/questions/86766/how-to-properly-handle-exceptions-when-performing-file-io
             * try
             * {
             *  using (FileStream outFile = new FileStream("customers.txt", FileMode.Append, FileAccess.Write))
             *  using (StreamWriter writer = new StreamWriter(outFile))
             *  {
             *      // Write info to file.
             *      writer.WriteLine($"{FirstNameBox.Text},{LastNameBox.Text},{AddressBox.Text},{CityBox.Text},{StateBox.Text},{ZipBox.Text},{PhoneBox.Text},{EmailBox.Text}");
             *  }
             *
             *  // Clear Text Boxes.
             *  FirstNameBox.Clear();
             *  LastNameBox.Clear();
             *  AddressBox.Clear();
             *  CityBox.Clear();
             *  StateBox.Clear();
             *  ZipBox.Clear();
             *  PhoneBox.Clear();
             *  EmailBox.Clear();
             *
             *  // Update comboBox with new info.
             *  CustomerSelectBox.Items.Remove(CustomerSelectBox.SelectedItem);
             *  CustomerSelectBox.Items.Add(tempCustomer);
             * }
             * catch (FileNotFoundException)
             * {
             *  MessageBox.Show("File 'customers.txt' not found to write.", "File Write Error");
             * }
             * // Catch generic I/O exceptions.
             * catch (IOException ex)
             * {
             *  MessageBox.Show(ex.ToString(), "Error");
             * }
             */
        }
Beispiel #2
0
 private void Window_Activated(object sender, EventArgs e)
 {
     FirstNameBox.Focus();
 }