private void btnFirst_Click(object sender, EventArgs e) { Order1 order = bl.GetFirstOrder(); PopulateTextBoxes(order); dataGridView1.DataSource = bl.CreateDataTable(Convert.ToInt32(orderIDTextBox.Text)); }
//method to update db with new ship date private void btnUpdate_Click(object sender, EventArgs e) { //checks if the ship date field is empty if (bl.ValidateIsNull(txtShipDate.Text)) { //if ship date field is empty, constructs an order object with a null ship date Order1 updatedOrder = CreateOrderFromForm(bl.ConvertDateToNull()); //attempts to update database with new order object if (bl.UpdateShippedDate(updatedOrder)) { //if update is successful MessageBox.Show("Order updated successfully with null date"); } else { //if update is unsuccessful, this indicated a concurrency error has occurred. data refreshes with changed info and user is informed bl.RefreshDataBase(); Order1 newOrder = bl.GetOrderByID(Convert.ToInt32(orderIDTextBox.Text)); PopulateTextBoxes(newOrder); MessageBox.Show("Concurrency Error: Data has been altered or deleted"); } } //if the ship date field is not empty, checks if it is a valid datetime else if (bl.ValidateDateTimeEntryFormat(txtShipDate.Text)) { //if the date is entered in a valid format, it is checked to make sure it is in the valid range (after order date but before required date) if (bl.ValidateDateTimeEntryDate(Convert.ToDateTime(txtShipDate.Text), GetOrderDateTime(), GetRequiredDateTime())) { //if date is in acceptable range, order object is built from form and an attempt is made to update the database with it Order1 updatedOrder = CreateOrderFromForm(Convert.ToDateTime(txtShipDate.Text)); if (bl.UpdateShippedDate(updatedOrder)) { MessageBox.Show("Order updated successfully with valid date"); } //if update is unsuccessful, this indicated a concurrency error has occurred. data refreshes with changed info and user is informed else { bl.RefreshDataBase(); Order1 newOrder = bl.GetOrderByID(Convert.ToInt32(orderIDTextBox.Text)); PopulateTextBoxes(newOrder); MessageBox.Show("Concurrency Error: Data has been altered or deleted"); } } //date failed range validation else { txtShipDate.Text = ""; txtShipDate.Focus(); MessageBox.Show("Please enter a valid date that is after the order date and before the required date"); } } //date failed format validation else { txtShipDate.Text = ""; txtShipDate.Focus(); MessageBox.Show("Please enter a valid date in the form yyyy-mm-dd"); } }
private void btnPrev_Click(object sender, EventArgs e) { int orderId = Convert.ToInt32(orderIDTextBox.Text); Order1 order = bl.GetPreviousOrder(orderId); PopulateTextBoxes(order); dataGridView1.DataSource = bl.CreateDataTable(Convert.ToInt32(orderIDTextBox.Text)); }
//used to check if a searched order exists (is not null) public bool ValidateOrderExists(Order1 order) { if (order != null) { return(true); } else { return(false); } }
//method to populate form with data from order object public void PopulateTextBoxes(Order1 order) { orderIDTextBox.Text = Convert.ToString(order.OrderID); string orderDate = $"{order.OrderDate:yyyy-MM-dd}"; customerIDTextBox.Text = Convert.ToString(order.CustomerID); string reqDate = $"{order.RequiredDate:yyyy-MM-dd}"; string shipDate = $"{order.ShippedDate:yyyy-MM-dd}"; txtOrderDate.Text = orderDate; txtReqDate.Text = reqDate; txtShipDate.Text = shipDate; }
//method to use data in form to construct an order object (allows nulls in shipdate) public Order1 CreateOrderFromForm(DateTime?shipDate) { int orderID = Convert.ToInt32(orderIDTextBox.Text); DateTime orderDate = Convert.ToDateTime(txtOrderDate.Text); string customerID = customerIDTextBox.Text; DateTime reqDate = Convert.ToDateTime(txtReqDate.Text); var newOrder = new Order1 { OrderID = orderID, OrderDate = orderDate, CustomerID = customerID, RequiredDate = reqDate, ShippedDate = shipDate }; return(newOrder); }
//method to update db with new data public bool UpdateShippedDate(Order1 oldOrder) { //creates a new order object based on updated order orderId var newOrder = db.Order1s.First(o => o.OrderID == oldOrder.OrderID); //changes ship date in order to new value newOrder.ShippedDate = oldOrder.ShippedDate; try { //attempts to update database with new information db.SubmitChanges(); return(true); } //if a concurrency error exists, a changeconflictexception will be thrown and the method will return false, telling the form that a concurrency error has occured catch (System.Data.Linq.ChangeConflictException) { return(false); } }
//use order id to search db for matching order public Order1 GetOrderByID(int id) { Order1 order = db.Order1s.FirstOrDefault(x => x.OrderID == id); return(order); }