// CUSTOMER SEARCH BUTTON click event private void btn_CustomerSearch_Click(object sender, EventArgs e) { // Make sure all controls are visible gbx_SearchCriteria.Visible = true; btn_ViewOrders.Visible = true; btn_CreateCustomer.Visible = true; btn_CustomerSearch.Visible = true;; btn_MainPage.Visible = true; btn_EnableEditing.Visible = true; btn_NewOrder.Visible = true; btn_SaveChanges.Visible = false; lbl_CustomerResultSort.Visible = false; //Make all customer data fields read-only by default MakeReadOnly(); //Reset form to prepare for new search results ClearResults(); // Verify that a query type was selected by the user if ((cbx_CustomerSearchType.SelectedItem == null) || (tbx_CustomerSearchInput.Text == null)) { ApplicationObjects.DisplayInvalidInput("Please make sure all search criterea is filled out."); return; } // SEARCH BY CUSTOMER FIRST NAME - Chech to see if user entered a customer first name else if (cbx_CustomerSearchType.SelectedItem.ToString() == "First Name") { // Generate a list of customers that match the first name entered by the user customers = ApplicationObjects.GetCustomerByFirstName(tbx_CustomerSearchInput.Text); // If customer list is empty, display no results found if (customers == null) { lbl_CustomerResultsFound.Text = "No results found!"; } else { // Populate the search results combo box with result first and last names foreach (Customer customer in customers) { cbx_CustomerResultsList.Items.Add(customer.FirstName + " " + customer.LastName); } // Change result label to show how results are listed in the results combobox lbl_CustomerResultSort.Text = "results listed by [first name] [last name]"; lbl_CustomerResultsFound.Text = cbx_CustomerResultsList.Items.Count.ToString() + " results found!"; } } // SEARCH BY CUSTOMER LAST NAME - Check to see if user entered a customer last name else if (cbx_CustomerSearchType.SelectedItem.ToString() == "Last Name") { // Generate a list of customers that match the last name entered by the user customers = ApplicationObjects.GetCustomerByLastName(tbx_CustomerSearchInput.Text); if (customers == null) { lbl_CustomerResultsFound.Text = "No results found!"; } else { // Populate the search results combo box with result last, first names foreach (Customer customer in customers) { cbx_CustomerResultsList.Items.Add(customer.LastName + ", " + customer.FirstName); } // Change result label to show how results are listed in the results combobox lbl_CustomerResultSort.Text = "results listed by [last name], [first name]"; lbl_CustomerResultsFound.Text = cbx_CustomerResultsList.Items.Count.ToString() + " results found!"; } } // SEARCH BY CUSTOMER ID - Check to see if user entered a customer ID else if (cbx_CustomerSearchType.SelectedItem.ToString() == "Customer ID") { //if invalid Guid, display error if (!Guid.TryParse((tbx_CustomerSearchInput.Text), out guidValidation)) { ApplicationObjects.DisplayInvalidInput("Invalid customer ID. Please try again."); return; } //If valid Guid, continue with search else { // Generate a list of customers that match the customer ID entered by the user customers = ApplicationObjects.GetCustomerByCustomerId(Guid.Parse(tbx_CustomerSearchInput.Text)); if (customers == null) { lbl_CustomerResultsFound.Text = "No results found!"; } else { // Populate the search results combo box with result last, first names foreach (Customer customer in customers) { cbx_CustomerResultsList.Items.Add(customer.CustomerId.ToString()); } // Change result label to show how results are listed in the results combobox lbl_CustomerResultSort.Text = "results listed by [customer id]"; lbl_CustomerResultsFound.Text = cbx_CustomerResultsList.Items.Count.ToString() + " results found!"; } } } // SEARCH BY CUSTOMER PHONE NUMBER - Check to see if user entered a customer phone number else if (cbx_CustomerSearchType.SelectedItem.ToString() == "Phone Number") { // Validate numeric input for phone number if ((!long.TryParse((tbx_CustomerSearchInput.Text), out longValidation)) || (tbx_CustomerSearchInput.Text.Length != 10)) { ApplicationObjects.DisplayInvalidInput("Invalid phone number entered. Please enter 10 digits (no dashes) & try again."); return; } else { // Generate a list of customers that match the phone number entered by the user customers = ApplicationObjects.GetCustomerByPhoneNumber(tbx_CustomerSearchInput.Text); // If customer list is empty, display no results found if (customers == null) { lbl_CustomerResultsFound.Text = "No results found!"; } else { // Populate the search results combo box with result last, first names foreach (Customer customer in customers) { cbx_CustomerResultsList.Items.Add(customer.PhoneNumber); } // Change result label to show how results are listed in the results combobox lbl_CustomerResultSort.Text = "results listed by [phone number]"; lbl_CustomerResultsFound.Text = cbx_CustomerResultsList.Items.Count.ToString() + " results found!"; } } } // SEARCH BY CUSTOMER EMAIL ADDRESS - Check to see if user entered a customer email address else if (cbx_CustomerSearchType.SelectedItem.ToString() == "Email Address") { // validate email address format if (!ApplicationObjects.EmailIsValid(tbx_CustomerSearchInput.Text)) { ApplicationObjects.DisplayInvalidInput("Invalid e-mail address entered. Please try again."); return; } else { // Generate a list of customers that match the email address entered by the user customers = ApplicationObjects.GetCustomerByEmail(tbx_CustomerSearchInput.Text); // If customer list is empty, display no results found if (customers == null) { lbl_CustomerResultsFound.Text = "No results found!"; } else { // Populate the search results combo box with result last, first names foreach (Customer customer in customers) { cbx_CustomerResultsList.Items.Add(customer.EmailAddress); } // Change result label to show how results are listed in the results combobox lbl_CustomerResultSort.Text = "results listed by [email address]"; lbl_CustomerResultsFound.Text = cbx_CustomerResultsList.Items.Count.ToString() + " results found!"; } } } // If the customer list is empty (no results found) if (customers.Count == 0) { // Display no results found lbl_CustomerResultsFound.Text = "No results found!"; return; } // If customer list is not empty, display results else { lbl_CustomerResultSort.Visible = true; cbx_CustomerResultsList.Visible = true; cbx_CustomerResultsList.SelectedIndex = 0; //Populate Customer details for first search result PopulateCustomerDetailsWithSelectedIndex(); } }