/// <summary> /// Validates all user inputs, then saves new order to order table /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOrdersAddGo_Click(object sender, EventArgs e) { bool custCheck = false; //Creating needed classes Orders ord = new Orders(); Validation val = new Validation(); //Running validation and saving validated input to object class ord.Date = ordersDatePk.Text; ord.CustID = val.numValidate(txtCustIDOrdersAdd.Text); //Creating List to hold all customer objects. List <Customer> allCustomers = new List <Customer>(); //Get all customers from the database. allCustomers = CustomerSQL.LoadCustomers(); foreach (Customer c in allCustomers) { if (c.CustID == ord.CustID) { custCheck = true; } } //If all input is valid (returns something other than "" -1) saves results and displays conformation message if (ord.CustID != -1 && ord.Date != "" && custCheck == true) { OrdersSQL.SaveOrder(ord); MessageBox.Show("Successfully added order with Customer ID " + ord.CustID + "\nand date " + ord.Date); } //Else program will cancel the save, and display text fields that caused the error else if (ord.CustID == -1 || ord.Date == "") { string error = "Add canceled due to error in the following fields:"; if (ord.Date == "") { error += " \n Date"; } if (ord.CustID == -1) { error += " \n Customer ID"; } error += " \n \nPlease ensure all fields are not empty and have proper input."; MessageBox.Show(error); } else { string error = "Add canceled; the provided Customer ID was not found."; MessageBox.Show(error); } }
/// <summary> /// Validates all user inputs, then deletes given record /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCustomerDeleteGo_Click(object sender, EventArgs e) { //Creating needed classes Customer cust = new Customer(); Validation val = new Validation(); //Running validation and saving validated input to object class cust.CustID = val.numValidate(txtIDCustomerDelete.Text); //If input is valid... if (cust.CustID != -1) { //Bool to check whether given ID exists in database bool idExist = false; //Creating List to hold all customer objects. List <Customer> allCustomers = new List <Customer>(); //Get all customers from the database. allCustomers = CustomerSQL.LoadCustomers(); //Loops through database and checks to see if given ID exists foreach (Customer c in allCustomers) { if (c.CustID == cust.CustID) { idExist = true; //Fills out first and last name of customer if ID is found cust.FirstName = c.FirstName; cust.LastName = c.LastName; } } //If ID is found, deletes record and displays conformation message if (idExist == true) { CustomerSQL.DeleteCustomer(cust); MessageBox.Show("Successfully deleted " + cust.FirstName + " " + cust.LastName); } //Else program will cancel the delete, and display error message else { MessageBox.Show("Delete cancelled; provided ID could not be found in database."); } } //Else program will cancel the delete, and display error message else { MessageBox.Show("Delete cancelled due to error in Customer ID field." + "\n\nPlease ensure field is not empty and has proper input."); } }
/// <summary> /// On form load, loads all records from database and displays them in data grid. (Also called by Refresh button) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CustomersMain_Load(object sender, EventArgs e) { //Resetting search boxes for Refresh button txtBoxSearchCustomer.Text = ""; txtBoxCustomerSearchID.Text = ""; txtBoxSearchCustomerLName.Text = ""; txtBoxCustomerSearchEmail.Text = ""; //Creating List to hold all customer objects. List <Customer> allCustomers = new List <Customer>(); //Get all customers from the database. allCustomers = CustomerSQL.LoadCustomers(); //Creating DataTable object to present the Customer Table from the database. DataTable customersTable = new DataTable(); //Adding the Rows that we are going to display. customersTable.Columns.Add("ID"); customersTable.Columns.Add("FirstName"); customersTable.Columns.Add("LastName"); customersTable.Columns.Add("Email"); //Adding object from the allCustomers list as row in our Data Table. foreach (var customer in allCustomers) { customersTable.Rows.Add(customer.CustID, customer.FirstName, customer.LastName, customer.Email); } //Filling our Data Table in a DataView so we can give it to our DataGrid. customerView = new DataView(customersTable); //Dynamically adjust the width of the DataGrid depending on how many columns we have. dataGridCustomer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //Adding the DataView with our Customers to the DataGrid dataGridCustomer.DataSource = customerView; //Making the DataGrid read only. dataGridCustomer.ReadOnly = true; //Removing the option for users to add directly into the database. dataGridCustomer.AllowUserToAddRows = false; }