public int createOrder(Order order) { Int32 orderNumber = 0; using (OleDbConnection sqlconn = new OleDbConnection(database)) { try { sqlconn.Open(); OleDbCommand cmd = sqlconn.CreateCommand(); OleDbTransaction transact = sqlconn.BeginTransaction(); cmd.Transaction = transact; String insert = "INSERT INTO [ORDERS]([user_id], [payment_id], [validated], [completed], [payment_amount], [collect_on_delivery]) " + "VALUES (@userId, @paymentId, false, false, @paymentAmount, @collectOnDelivery)"; cmd.CommandText = insert; cmd.Parameters.Add("userId", OleDbType.Integer).Value = order.getPaymentInformation().getUser().getId(); cmd.Parameters.Add("paymentId", OleDbType.Integer).Value = order.getPaymentInformation().getPaymentInformationId(); cmd.Parameters.Add("paymentAmount", OleDbType.Double).Value = order.getPaymentAmount(); cmd.Parameters.Add("collectOnDelivery", OleDbType.Boolean).Value = order.getCollectOnDelivery(); cmd.Prepare(); int rows = cmd.ExecuteNonQuery(); if (rows != 1) { transact.Rollback(); } cmd.Parameters.Clear(); String select = "SELECT LAST([order_id]) FROM [ORDERS]"; cmd.CommandText = select; cmd.Prepare(); orderNumber = (int)cmd.ExecuteScalar(); order.setOrderId(orderNumber); List <OrderItem> items = order.getOrderItems(); foreach (OrderItem item in items) { cmd.Parameters.Clear(); String insert2 = "INSERT INTO [ORDER_ITEMS]([order_id], [product_id], [count], [sale_price]) " + "VALUES(@orderId, @productId, @count, @salePrice)"; cmd.CommandText = insert2; cmd.Parameters.Add("orderId", OleDbType.Integer).Value = item.getOrderId(); cmd.Parameters.Add("productId", OleDbType.Integer).Value = item.getProductId(); cmd.Parameters.Add("count", OleDbType.Integer).Value = item.getCount(); cmd.Parameters.Add("salePrice", OleDbType.Double).Value = item.getSalePrice(); cmd.Prepare(); int rows2 = cmd.ExecuteNonQuery(); if (rows2 != 1) { transact.Rollback(); } } transact.Commit(); return(orderNumber); } catch (OleDbException ex) { orderNumber = -1; return(orderNumber); } finally { sqlconn.Close(); } } }
public int createOrder(Order order) { Int32 orderNumber = 0; using (OleDbConnection sqlconn = new OleDbConnection(database)) { try { sqlconn.Open(); OleDbCommand cmd = sqlconn.CreateCommand(); OleDbTransaction transact = sqlconn.BeginTransaction(); cmd.Transaction = transact; String insert = "INSERT INTO [ORDERS]([user_id], [payment_id], [validated], [completed], [payment_amount], [collect_on_delivery]) " + "VALUES (@userId, @paymentId, false, false, @paymentAmount, @collectOnDelivery)"; cmd.CommandText = insert; cmd.Parameters.Add("userId", OleDbType.Integer).Value = order.getPaymentInformation().getUser().getId(); cmd.Parameters.Add("paymentId", OleDbType.Integer).Value = order.getPaymentInformation().getPaymentInformationId(); cmd.Parameters.Add("paymentAmount", OleDbType.Double).Value = order.getPaymentAmount(); cmd.Parameters.Add("collectOnDelivery", OleDbType.Boolean).Value = order.getCollectOnDelivery(); cmd.Prepare(); int rows = cmd.ExecuteNonQuery(); if (rows != 1) { transact.Rollback(); } cmd.Parameters.Clear(); String select = "SELECT LAST([order_id]) FROM [ORDERS]"; cmd.CommandText = select; cmd.Prepare(); orderNumber = (int)cmd.ExecuteScalar(); order.setOrderId(orderNumber); List<OrderItem> items = order.getOrderItems(); foreach (OrderItem item in items) { cmd.Parameters.Clear(); String insert2 = "INSERT INTO [ORDER_ITEMS]([order_id], [product_id], [count], [sale_price]) " + "VALUES(@orderId, @productId, @count, @salePrice)"; cmd.CommandText = insert2; cmd.Parameters.Add("orderId", OleDbType.Integer).Value = item.getOrderId(); cmd.Parameters.Add("productId", OleDbType.Integer).Value = item.getProductId(); cmd.Parameters.Add("count", OleDbType.Integer).Value = item.getCount(); cmd.Parameters.Add("salePrice", OleDbType.Double).Value = item.getSalePrice(); cmd.Prepare(); int rows2 = cmd.ExecuteNonQuery(); if (rows2 != 1) { transact.Rollback(); } } transact.Commit(); return orderNumber; } catch (OleDbException ex) { orderNumber = -1; return orderNumber; } finally { sqlconn.Close(); } } }
private void loadSingleOrder(int viewOrderId) { OrderManager businessLayer = new OrderManager(); cisseniorproject.dataobjects.Order order = businessLayer.getOrder(viewOrderId); lblUserId.Text = order.getUserId().ToString(); lblUsername.Text = order.getPaymentInformation().getUser().getUsername(); lblFirstName.Text = order.getPaymentInformation().getUser().getFirstName(); lblLastName.Text = order.getPaymentInformation().getUser().getLastName(); lblAddress.Text = order.getPaymentInformation().getUser().getAddress(); lblCity.Text = order.getPaymentInformation().getUser().getCity(); lblState.Text = order.getPaymentInformation().getUser().getState(); lblZipCode.Text = order.getPaymentInformation().getUser().getZipCode(); lblAccountCreationDate.Text = order.getPaymentInformation().getUser().getAccountCreationDate().ToShortDateString(); lblEmail.Text = order.getPaymentInformation().getUser().getEamil(); lblCreditCardType.Text = order.getPaymentInformation().getCreditCardType(); lblCreditCardNumber.Text = order.getPaymentInformation().getCreditCardNumber(); lblCardCity.Text = order.getPaymentInformation().getCity(); lblCardState.Text = order.getPaymentInformation().getState(); lblCardExpDate.Text = order.getPaymentInformation().getCardExpDate().ToShortDateString(); lblCardSecurityCode.Text = order.getPaymentInformation().getSecurityCode(); lblOrderId.Text = order.getOrderId().ToString(); lblOrderUserId.Text = order.getUserId().ToString(); cbIsValidated.Checked = order.isOrderValidated(); cbIsCompleted.Checked = order.isOrderCompleted(); cbIsCollectOnDelivery.Checked = order.getCollectOnDelivery(); lblPaymentAmount.Text = String.Format("{0:C}", order.getPaymentAmount()); lblTotalOrderAmount.Text = String.Format("{0:C}", businessLayer.getOrderTotal()); lblAmountDue.Text = String.Format("{0:C}", businessLayer.getAmountDue()); List <UserOrderItem> orderItems = businessLayer.getOrderItems(order); rptOrderItems.DataSource = orderItems; rptOrderItems.DataBind(); }