Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear friend ,

I am having Some problem while saving data into database

Problem Summery.....

Suppose I am inserting
C#
newOrder.AddItem(1,1,2,120,12);
                       newOrder.AddItem(1, 2, 2, 130, 12);
                       newOrder.AddItem(1, 3, 2, 140, 12);
                       newOrder.AddItem(1, 4, 2, 150, 12);


this data first added to the icollection..

then saved to the database...

so only four record should be Added to the database

but 7 records are saved into database....

eg: orderid ItemId Quantity Price,Discount
1 1 2 120 12
1 2 2 130 12
1 3 2 140 12
1 4 2 150 12
1 2 2 130 12
1 3 2 140 12
1 4 2 150 12


from record no 2 it is getting repeated...


the boalded records are repeated ....

My code is give below

I call GenerateOrder()

C#
public void GenerateOrder()
        {
            DHPShoppingCartEntities db = new DHPShoppingCartEntities();
             Order newOrder;
            string CustomerNAme = "Pranay";
            string ShippingAddress = "Shipping Address Of The Customer";
            string BillingAddress = "Billing Address Of The Customer";
            DateTime OrderDate = DateTime.Now;
            string OrderStatus = ShoppingCartLib.OrderStatus.Placed.ToString();
            short DiscountCode = 12;

            using (TransactionScope transactionscope = new TransactionScope())
            {
                bool success = false;
                try
                {
                    newOrder = new Order(CustomerNAme, ShippingAddress, BillingAddress, OrderDate, DiscountCode);

                    long oid = newOrder.PersistOrder();
                
                 
                        newOrder.AddItem(oid,1,2,120,12);
                        newOrder.AddItem(oid, 1, 2, 130, 12);
                        newOrder.AddItem(oid, 1, 2, 140, 12);
                        newOrder.AddItem(oid, 1, 2, 150, 12);
                  
                    newOrder.InsertOrderDetails();
                
                    //foreach (CartItem cartitem in items)
                    //{

                    //}

                    transactionscope.Complete();
                    success = true;                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occured. "+ "The operation cannot be retried." + ex.Message);                
                }
                if (success)
                    db.SaveChanges();
                else
                    Console.WriteLine("The operation could not be completed");

                // Dispose the object db.
                db.Dispose();
            }           
        }



there is a Order Class in which i added items into icollection into sc_Order Table

sc_Order orderhead = new sc_Order();

sc_OrderDetails Table Have sc_Order Primary Key

C#
public void AddItem(long orderid, int itemID, int quantity, decimal price, short discountcode)
      {
          sc_OrderDetails item = new sc_OrderDetails();

          item.ItemId = itemID;
          item.Quantity = quantity;
          item.Price = price;
          item.DiscountCode = discountcode;
          item.OrderId = orderid;

          orderhead.sc_OrderDetails.Add(item);

      }


XML
below code save Order into <b>sc_Order Table</b> and<b> return orderid</b>


public long PersistOrder()
       {
           db.sc_Order.Add(orderhead);
           db.SaveChanges();

           long id = (orderhead.OrderId);
           return id;
       }

       public void InsertOrderDetails()
       {
           foreach (sc_OrderDetails orderdetails in orderhead.sc_OrderDetails)
           {
               db.sc_OrderDetails.Add(orderdetails);
               db.SaveChanges();
           }
       }

Please give solution for this problem...as soon as possible...


Thanks in Advance
Posted
Updated 31-Jul-13 1:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900