Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Deleting orders - one more time
Message
 
 
À
27/05/2009 16:07:05
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01402330
Message ID:
01402341
Vues:
40
I'm thinking I programmed everything wrong. Obviously Biz object should be able itself to delete order and its details. Also in the way I implemented it now everything seems to be coupled - interface should not go through details of accessing DataSet row and passing PK, I think.

Let me first understand how should I have programmed it and then try to fix the problem.

In the meantime, this is the whole Order Biz
 public class OrderBiz
    {
        public OrderBiz()
        { 
        }

        /// <summary>
        /// Has Changes
        ///     are there any changes to the data?
        /// </summary>
        /// <returns></returns>
        public bool HasChanges(OrdersDataSet.OrdersDataTable dt)
        {
            bool retVal = false;

            DataTable dtChanged = dt.GetChanges();
            if (dtChanged != null && dtChanged.Rows.Count > 0)
            {
                retVal = true;
            }

            return retVal;

        }


        /// <summary>
        /// Do order search
        ///     returns all orders matching the search criteria.
        /// </summary>
        /// <param name="startDate">Order start date</param>
        /// <param name="endDate">Order end date</param>
        /// <returns>DataTable</returns>
        public OrdersDataSet.OrdersDataTable Find(DateTime startDate, DateTime endDate)
        {
            OrdersDataSet.OrdersDataTable  dt = new OrdersDataSet.OrdersDataTable();
            using (OrdersTableAdapter ta = new OrdersTableAdapter())
            {                
                dt = ta.GetOrdersByDate(startDate, endDate);
            }

            return dt;
        }

        /// <summary>
        /// Insert new order      
        /// </summary>
        /// <param name="dr">data row</param>
        /// <returns>ID of new record</returns>
        public int InsertNewOrder(OrdersDataSet.OrdersRow dr)
        {
            int retVal;
            using (OrdersTableAdapter ta = new OrdersTableAdapter())
            {
                dr.OrderPk  = Guid.NewGuid();

                retVal = (int)ta.InsertNewOrder(dr.OrderPk);
            }
        
            return retVal;
        }


        ///// <summary>
        ///// Get Inserted key
        /////     return the last key assigned by the insert clause.
        ///// </summary>
        ///// <returns>order id</returns>
        //public long GetInsertedKey()
        //{
        //    OrdersTableAdapter ta = new OrdersTableAdapter();

        //    return (long)ta.GetInsertedKey();
        //}
        /// <summary>
        /// Delete Order 
        /// </summary>
        /// <param name="OrderKey"></param>
        /// <returns></returns>
        public int DeleteOrder(Guid OrderKey)
        {
            OrdersTableAdapter ta = new OrdersTableAdapter();
            return ta.Delete(OrderKey);  
        }
        
        /// <summary>
        /// Update order table
        /// </summary>
        /// <param name="dt">datatable to update</param>
        /// <returns>number of records updated</returns>
        public int UpdateOrderTable(OrdersDataSet.OrdersDataTable dt)
        {
            OrdersTableAdapter ta = new OrdersTableAdapter();
            return ta.Update(dt);            
        }

        public OrdersDataSet.OrdersDataTable GetOrderByOrderKey(Guid orderKey)
        {
            OrdersTableAdapter ta = new OrdersTableAdapter();
            return ta.GetOrderByPK(orderKey);            
         }

        /// <summary>
        /// Get Orders Total
        ///     returns the Total of all in all orders.
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public decimal GetOrdersTotal(OrdersDataSet.OrdersDataTable dt)
        {
            decimal Total = 0.00M;
            foreach (OrdersDataSet.OrdersRow dr in dt.Rows)
            {
                if (!dr.RowState.Equals(DataRowState.Deleted) || !dr.RowState.Equals(DataRowState.Detached))
                {
                    Total += dr.TotalPrice;
                }
            }

            return Total;
        }
    }
the _dtSearch is a form's property

// Used for search results
private OrdersDataSet.OrdersDataTable _dtOrderSearch = new OrdersDataSet.OrdersDataTable();

and the DataGrid uses it as its source.

>>Hi everybody,
>>
>>Sorry for repeat.
>>
>>This is what I have right now, but it is not working:
>>
>> /// <summary>
>>        /// Delete menu item click event
>>        /// </summary>
>>        private void DeleteMenuItem_Click(object sender, EventArgs e)        {
>>
>>            int _rowNumber = this.grdSearch._RowNumber;
>>            if (this.tcOrders.SelectedIndex == 2 && _rowNumber  >= 0) //Search Page
>>            {    
>>
>>                DialogResult dr = MessageBox.Show("Are you sure to delete this order?", "Delete Order", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
>>                if (dr == DialogResult.Yes)
>>                {
>>                    try
>>                    {
>>                        //delete items
>>                        Guid OrderPk = ((OrdersDataSet.OrdersRow)_dtOrderSearch.Rows[_rowNumber]).OrderPk;
>>                        _itemBiz.DeleteItemsByOrderKey(OrderPk);
>>                        //delete order
>>                    //    _OrderBiz.DeleteOrder(OrderPk);  
>>
>>                        _dtOrderSearch.Rows[_rowNumber].Delete();
>>                        _dtOrderSearch.AcceptChanges();
>>                        UpdateOrdersTotals(_dtOrderSearch);  
>>                    }
>>                    catch (Exception ex)
>>                    {
>>                        MessageBox.Show(ex.Message);
>>                    }
>>                }
>>            }
>>
>>
>>Everything seems to be correctly deleted. However, when I search again I see all my orders back but with 0 items. How can I actually delete the Order - why Delete and AcceptChanges don't do this?
>>
>>Thanks in advance.
>
>Is _itemBiz.DeleteItemsByOrderKey a method on a business object? Why not use the same methodology for deleting the actual order as well. Also, you should verify the line items were deleted before attempting to delete the actual order. What is _dtOrderSearch? If this is a grid, then that explains why you are seeing the order disappear in the grid but not deleted in the data.
>Tim
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform