/// <summary> /// Update an existing row in the datasource. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Order object to update.</param> /// <remarks> /// After updating the datasource, the PetShop.Business.Order object will be updated /// to refelect any changes made by the datasource. (ie: identity or computed columns) /// </remarks> /// <returns>Returns true if operation is successful.</returns> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override bool Update(TransactionManager transactionManager, PetShop.Business.Order entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Orders_Update", _useStoredProcedure); database.AddInParameter(commandWrapper, "@OrderId", DbType.Int32, entity.OrderId); database.AddInParameter(commandWrapper, "@UserId", DbType.AnsiString, entity.UserId); database.AddInParameter(commandWrapper, "@OrderDate", DbType.DateTime, entity.OrderDate); database.AddInParameter(commandWrapper, "@ShipAddr1", DbType.AnsiString, entity.ShipAddr1); database.AddInParameter(commandWrapper, "@ShipAddr2", DbType.AnsiString, entity.ShipAddr2); database.AddInParameter(commandWrapper, "@ShipCity", DbType.AnsiString, entity.ShipCity); database.AddInParameter(commandWrapper, "@ShipState", DbType.AnsiString, entity.ShipState); database.AddInParameter(commandWrapper, "@ShipZip", DbType.AnsiString, entity.ShipZip); database.AddInParameter(commandWrapper, "@ShipCountry", DbType.AnsiString, entity.ShipCountry); database.AddInParameter(commandWrapper, "@BillAddr1", DbType.AnsiString, entity.BillAddr1); database.AddInParameter(commandWrapper, "@BillAddr2", DbType.AnsiString, entity.BillAddr2); database.AddInParameter(commandWrapper, "@BillCity", DbType.AnsiString, entity.BillCity); database.AddInParameter(commandWrapper, "@BillState", DbType.AnsiString, entity.BillState); database.AddInParameter(commandWrapper, "@BillZip", DbType.AnsiString, entity.BillZip); database.AddInParameter(commandWrapper, "@BillCountry", DbType.AnsiString, entity.BillCountry); database.AddInParameter(commandWrapper, "@Courier", DbType.AnsiString, entity.Courier); database.AddInParameter(commandWrapper, "@TotalPrice", DbType.Decimal, entity.TotalPrice); database.AddInParameter(commandWrapper, "@BillToFirstName", DbType.AnsiString, entity.BillToFirstName); database.AddInParameter(commandWrapper, "@BillToLastName", DbType.AnsiString, entity.BillToLastName); database.AddInParameter(commandWrapper, "@ShipToFirstName", DbType.AnsiString, entity.ShipToFirstName); database.AddInParameter(commandWrapper, "@ShipToLastName", DbType.AnsiString, entity.ShipToLastName); database.AddInParameter(commandWrapper, "@AuthorizationNumber", DbType.Int32, entity.AuthorizationNumber); database.AddInParameter(commandWrapper, "@Locale", DbType.AnsiString, entity.Locale); int results = 0; //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Update", entity)); if (transactionManager != null) { results = Utility.ExecuteNonQuery(transactionManager, commandWrapper); } else { results = Utility.ExecuteNonQuery(database, commandWrapper); } //Stop Tracking Now that it has been updated and persisted. if (DataRepository.Provider.EnableEntityTracking) { EntityManager.StopTracking(entity.EntityTrackingKey); } entity.AcceptChanges(); //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Update", entity)); return(Convert.ToBoolean(results)); }
/// <summary> /// Inserts a PetShop.Business.Order object into the datasource using a transaction. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Order object to insert.</param> /// <remarks> /// After inserting into the datasource, the PetShop.Business.Order object will be updated /// to refelect any changes made by the datasource. (ie: identity or computed columns) /// </remarks> /// <returns>Returns true if operation is successful.</returns> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override bool Insert(TransactionManager transactionManager, PetShop.Business.Order entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Orders_Insert", _useStoredProcedure); database.AddOutParameter(commandWrapper, "@OrderId", DbType.Int32, 4); database.AddInParameter(commandWrapper, "@UserId", DbType.AnsiString, entity.UserId); database.AddInParameter(commandWrapper, "@OrderDate", DbType.DateTime, entity.OrderDate); database.AddInParameter(commandWrapper, "@ShipAddr1", DbType.AnsiString, entity.ShipAddr1); database.AddInParameter(commandWrapper, "@ShipAddr2", DbType.AnsiString, entity.ShipAddr2); database.AddInParameter(commandWrapper, "@ShipCity", DbType.AnsiString, entity.ShipCity); database.AddInParameter(commandWrapper, "@ShipState", DbType.AnsiString, entity.ShipState); database.AddInParameter(commandWrapper, "@ShipZip", DbType.AnsiString, entity.ShipZip); database.AddInParameter(commandWrapper, "@ShipCountry", DbType.AnsiString, entity.ShipCountry); database.AddInParameter(commandWrapper, "@BillAddr1", DbType.AnsiString, entity.BillAddr1); database.AddInParameter(commandWrapper, "@BillAddr2", DbType.AnsiString, entity.BillAddr2); database.AddInParameter(commandWrapper, "@BillCity", DbType.AnsiString, entity.BillCity); database.AddInParameter(commandWrapper, "@BillState", DbType.AnsiString, entity.BillState); database.AddInParameter(commandWrapper, "@BillZip", DbType.AnsiString, entity.BillZip); database.AddInParameter(commandWrapper, "@BillCountry", DbType.AnsiString, entity.BillCountry); database.AddInParameter(commandWrapper, "@Courier", DbType.AnsiString, entity.Courier); database.AddInParameter(commandWrapper, "@TotalPrice", DbType.Decimal, entity.TotalPrice); database.AddInParameter(commandWrapper, "@BillToFirstName", DbType.AnsiString, entity.BillToFirstName); database.AddInParameter(commandWrapper, "@BillToLastName", DbType.AnsiString, entity.BillToLastName); database.AddInParameter(commandWrapper, "@ShipToFirstName", DbType.AnsiString, entity.ShipToFirstName); database.AddInParameter(commandWrapper, "@ShipToLastName", DbType.AnsiString, entity.ShipToLastName); database.AddInParameter(commandWrapper, "@AuthorizationNumber", DbType.Int32, entity.AuthorizationNumber); database.AddInParameter(commandWrapper, "@Locale", DbType.AnsiString, entity.Locale); int results = 0; //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Insert", entity)); if (transactionManager != null) { results = Utility.ExecuteNonQuery(transactionManager, commandWrapper); } else { results = Utility.ExecuteNonQuery(database, commandWrapper); } object _orderId = database.GetParameterValue(commandWrapper, "@OrderId"); entity.OrderId = (int)_orderId; entity.AcceptChanges(); //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Insert", entity)); return(Convert.ToBoolean(results)); }
private void Child_Update(Order order, SqlConnection connection) { bool cancel = false; OnChildUpdating(order, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "UPDATE [dbo].[LineItem] SET [OrderId] = @p_OrderId, [LineNum] = @p_LineNum, [ItemId] = @p_ItemId, [Quantity] = @p_Quantity, [UnitPrice] = @p_UnitPrice WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum; SELECT [OrderId], [LineNum] FROM [dbo].[LineItem] WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalOrderId", order != null ? order.OrderId : this.OriginalOrderId); command.Parameters.AddWithValue("@p_OrderId", order != null ? order.OrderId : this.OrderId); command.Parameters.AddWithValue("@p_OriginalLineNum", this.OriginalLineNum); command.Parameters.AddWithValue("@p_LineNum", this.LineNum); command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_Quantity", this.Quantity); command.Parameters.AddWithValue("@p_UnitPrice", this.UnitPrice); // result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(order != null && order.OrderId != this.OrderId) LoadProperty(_orderIdProperty, order.OrderId); // Update non-identity primary key value. LoadProperty(_originalOrderIdProperty, this.OrderId); // Update non-identity primary key value. LoadProperty(_originalLineNumProperty, this.LineNum); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildUpdated() and update this child manually. // FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
/// <summary> /// Process the order /// </summary> protected void wzdCheckOut_FinishButtonClick(object sender, WizardNavigationEventArgs e) { var inventoryService = new InventoryService(); Profile profile = ProfileManager.Instance.GetCurrentUser(Page.User.Identity.Name); if (profile.CartCollection.Count > 0) { // display ordered items CartListOrdered.Bind(profile.CartCollection); // display total and credit card information ltlTotalComplete.Text = ltlTotal.Text; ltlCreditCardComplete.Text = ltlCreditCard.Text; #region Create Order var order = new Order(); order.UserId = profile.UniqueId.ToString(); order.OrderDate = DateTime.Now; order.CreditCard = GetCreditCard(); order.Courier = order.CreditCard.CardType; order.TotalPrice = profile.CartCollection.Sum(c => c.Total); order.AuthorizationNumber = 0; order.Locale = "en-us"; #region Shipping Information order.ShipAddr1 = billingForm.Address.Address1; order.ShipAddr2 = billingForm.Address.Address2; order.ShipCity = billingForm.Address.City; order.ShipState = billingForm.Address.State; order.ShipZip = billingForm.Address.Zip; order.ShipCountry = billingForm.Address.Country; order.ShipToFirstName = billingForm.Address.FirstName; order.ShipToLastName = billingForm.Address.LastName; #endregion #region Billing Information order.BillAddr1 = shippingForm.Address.Address1; order.BillAddr2 = shippingForm.Address.Address2; order.BillCity = shippingForm.Address.City; order.BillState = shippingForm.Address.State; order.BillZip = shippingForm.Address.Zip; order.BillCountry = shippingForm.Address.Country; order.BillToFirstName = shippingForm.Address.FirstName; order.BillToLastName = shippingForm.Address.LastName; #endregion var orderService = new OrderService(); orderService.DeepSave(order); #endregion int itemsOnBackOrder = 0; //Decrement and check the Inventory. foreach (Cart cart in profile.CartCollection) { Inventory inventory = inventoryService.GetByItemId(cart.ItemId); if(cart.Quantity > inventory.Qty) { itemsOnBackOrder += cart.Quantity - inventory.Qty; } inventory.Qty -= cart.Quantity; #region Reset the Inventory back to 10,000 if (inventory.Qty < 0) inventory.Qty = 10000; #endregion inventoryService.DeepSave(inventory); } if(itemsOnBackOrder > 0) { ItemsOnBackOrder.Text = string.Format("<br /><p style=\"color:red;\"><b>Backorder ALERT:</b> {0} items are on backorder.</p>", itemsOnBackOrder); } var cartService = new CartService(); cartService.SaveOrderLineItems(order.OrderId, profile.CartCollection); //profile.CartCollection.SaveOrderLineItems(order.OrderId); // destroy cart cartService.Delete(profile.CartCollection); } else { lblMsg.Text = "<p><br>Can not process the order. Your cart is empty.</p><p class=SignUpLabel><a class=linkNewUser href=Default.aspx>Continue shopping</a></p>"; wzdCheckOut.Visible = false; } }