/// <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 btnOrdersDeleteGo_Click(object sender, EventArgs e) { //Creating needed classes Orders ord = new Orders(); Validation val = new Validation(); //Running validation and saving validated input to object class ord.OrderID = val.numValidate(txtIDOrdersDelete.Text); //If input is valid... if (ord.OrderID != -1) { OrdersSQL.DeleteOrder(ord.OrderID); } }
/// <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 OrdersMain_Load(object sender, EventArgs e) { //Resetting search box for Refresh button txtBoxSearchOrderID.Text = ""; txtBoxSearchOrderCustID.Text = ""; //Creating List to hold all order objects. List <Orders> allOrders = new List <Orders>(); //Get all orders from the database. allOrders = OrdersSQL.LoadOrders(); //Creating DataTable object to present the Customer Table from the database. DataTable ordersTable = new DataTable(); //Adding the Rows that we are going to display. ordersTable.Columns.Add("ID"); ordersTable.Columns.Add("Customer ID"); ordersTable.Columns.Add("Date"); //Adding object from the allOrders list as row in our Data Table. foreach (var orders in allOrders) { ordersTable.Rows.Add(orders.OrderID, orders.CustID, orders.Date); } //Filling our Data Table in a DataView so we can give it to our DataGrid. orderView = new DataView(ordersTable); //Dynamically adjust the width of the DataGrid depending on how many columns we have. dataGridOrder.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //Adding the DataView with our Customers to the DataGrid dataGridOrder.DataSource = orderView; //Making the DataGrid read only. dataGridOrder.ReadOnly = true; //Removing the option for users to add directly into the database. dataGridOrder.AllowUserToAddRows = false; }
private void btnOrderItemAddGo_Click(object sender, EventArgs e) { bool ordCheck = false; bool invCheck = false; //Creating needed classes OrderItem ordItem = new OrderItem(); Validation val = new Validation(); //Running validation and saving validated input to object class ordItem.invId = val.numValidate(txtInvIDOrderItemAdd.Text); ordItem.orderId = val.numValidate(txtOrderIDOrderItemAdd.Text); //Creating List to hold all inventory objects. List <Inventory> allInventory = new List <Inventory>(); //Get all items from the inventory table. allInventory = InventorySQL.LoadInventory(); foreach (Inventory i in allInventory) { if (i.InvID == ordItem.invId) { invCheck = true; } } //Creating List to hold all order objects. List <Orders> allOrders = new List <Orders>(); //Get all orders from the database. allOrders = OrdersSQL.LoadOrders(); foreach (Orders o in allOrders) { if (o.OrderID == ordItem.orderId) { ordCheck = true; } } //If all input is valid (returns something other than -1) saves results and displays conformation message if (ordItem.invId != -1 && ordItem.orderId != -1 && invCheck == true && ordCheck == true) { bool idCheck = false; //Creating List to hold all customer objects. List <OrderItem> allOrderItems = new List <OrderItem>(); //Get all customers from the database. allOrderItems = OrderItemSQL.LoadOrderItems(); foreach (OrderItem oi in allOrderItems) { if (oi.orderId == ordItem.orderId && oi.invId == ordItem.invId) { idCheck = true; } } if (idCheck == false) { OrderItemSQL.SaveOrderItem(ordItem); MessageBox.Show("Successfully added connection between Inventory ID " + ordItem.invId + " and Order ID " + ordItem.orderId); } else { MessageBox.Show("Add cancelled; provided IDs already exist in database."); } } else if (ordItem.invId == -1 || ordItem.orderId == -1) { string error = "Add canceled due to error in the following fields:"; if (ordItem.invId == -1) { error += " \n Inventory ID"; } if (ordItem.orderId == -1) { error += " \n Order ID"; } error += " \n \nPlease ensure all fields are not empty and have proper input."; MessageBox.Show(error); } else { string error = "Add canceled; the IDs input for the following text fields were not found:"; if (ordCheck == false) { error += "\n Order ID"; } if (invCheck == false) { error += "\n Inventory ID"; } error += "\n\nPlease ensure that all fields have existing IDs."; MessageBox.Show(error); } }