/// <summary>
        ///     Update an existing row in the datasource.
        /// </summary>
        /// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
        /// <param name="entity">PetShop.Business.LineItem object to update.</param>
        /// <remarks>
        ///		After updating the datasource, the PetShop.Business.LineItem 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.LineItem entity)
        {
            SqlDatabase database       = new SqlDatabase(this._connectionString);
            DbCommand   commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.LineItem_Update", _useStoredProcedure);

            database.AddInParameter(commandWrapper, "@OrderId", DbType.Int32, entity.OrderId);
            database.AddInParameter(commandWrapper, "@OriginalOrderId", DbType.Int32, entity.OriginalOrderId);
            database.AddInParameter(commandWrapper, "@LineNum", DbType.Int32, entity.LineNum);
            database.AddInParameter(commandWrapper, "@OriginalLineNum", DbType.Int32, entity.OriginalLineNum);
            database.AddInParameter(commandWrapper, "@ItemId", DbType.AnsiString, entity.ItemId);
            database.AddInParameter(commandWrapper, "@Quantity", DbType.Int32, entity.Quantity);
            database.AddInParameter(commandWrapper, "@UnitPrice", DbType.Decimal, entity.UnitPrice);

            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.OriginalOrderId = entity.OrderId;
            entity.OriginalLineNum = entity.LineNum;

            entity.AcceptChanges();

            //Provider Data Requested Command Event
            OnDataRequested(new CommandEventArgs(commandWrapper, "Update", entity));

            return(Convert.ToBoolean(results));
        }
Пример #2
0
        /// <summary>
        /// Saves the new order line items
        /// </summary>
        /// <param name="orderId">The order id of the item</param>
	    /// <param name="cartItems">The cart items</param>
	    public void SaveOrderLineItems(int orderId, TList<Cart> cartItems)
	    {
            var lineItemService = new LineItemService();
            var lineNum = 0;

            foreach (var item in cartItems)
            {
                var lineItem = new LineItem
                                   {
                                       OrderId = orderId,
                                       ItemId = item.ItemId,
                                       LineNum = ++lineNum,
                                       Quantity = item.Quantity,
                                       UnitPrice = item.Price
                                   };

                lineItemService.Save(lineItem);
            }

	    }
        protected override void DataPortal_Update()
        {
            bool cancel = false;
            OnUpdating(ref cancel);
            if (cancel) return;

            if(OriginalOrderId != OrderId || OriginalLineNum != LineNum)
            {
                // Insert new child.
                LineItem item = new LineItem {OrderId = OrderId, LineNum = LineNum, ItemId = ItemId, Quantity = Quantity, UnitPrice = UnitPrice};
                
                item = item.Save();

                // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships.

                // Create a new connection.
                using (var connection = new SqlConnection(ADOHelper.ConnectionString))
                {
                    connection.Open();
                    FieldManager.UpdateChildren(this, connection);
                }

                // Delete the old.
                var criteria = new LineItemCriteria {OrderId = OriginalOrderId, LineNum = OriginalLineNum};
                
                DataPortal_Delete(criteria);

                // Mark the original as the new one.
                OriginalOrderId = OrderId;
                OriginalLineNum = LineNum;

                OnUpdated();

                return;
            }

            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 connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using(var command = new SqlCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("@p_OriginalOrderId", this.OriginalOrderId);
                command.Parameters.AddWithValue("@p_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.");

                    LoadProperty(_originalOrderIdProperty, this.OrderId);
                    LoadProperty(_originalLineNumProperty, this.LineNum);
                }

                FieldManager.UpdateChildren(this, connection);
            }

            OnUpdated();
        }