Wiliam
Wiliam

Reputation: 27

How to add 2 products to sql at the same time

I have 2 tables:

Table order: IDorder, productId, quantity, price

Table orderdetails: ID, ShipName, ShipMobile, ShipAddress, ShipEmail, DateOrder.

raovatmuabanthucungEntities re = null;
        public OrderDao()
        {
            re = new raovatmuabanthucungEntities();
        }
        public bool Insert(order order)
        {
            try
            {
                re.order.Add(order);
                re.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

 public ActionResult Payment(string shipName, string mobile, string address, string email)
        {
            var detail = new orderdetail();
            detail.NgayDatHang = DateTime.Now;
            detail.ShipAddress = address;
            detail.ShipMobile = mobile;
            detail.ShipName = shipName;
            detail.ShipEmail = email;
            try
            {
                var id = new OrderDetailsDao().Insert(detail);
                var cart = (List<CartItem>)Session[CartSession];
                var orderDao = new OrderDao();
                decimal total = 0;
                foreach (var item in cart)
                {
                    var order = new order();
                    order.ProductId= item.Product.ProductId;
                    order.IDOrder = id;
                    order.Price = item.Product.Price;
                    order.quantity = item.quantity;
                    orderDao.Insert(order);
                }
            }
            catch (Exception ex)
            {
                //ghi log
                return Redirect("/loi-thanh-toan");
            }
            return Redirect("/hoan-thanh");
        }

Upvotes: 1

Views: 103

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 369

Actually in the line mentioned below :

order.IDOrder = id;

you are trying to insert duplicates into a primary Key field (as communicated by you in messages), so the first time it succeeds to insert value and the second time it fails to do so as duplicate primary keys are not allowed.

Also, re.SaveChanges(); is adding an over-head to your database write operation. It is better if you create an IEnumerable or List of "order" and pass it to Insert() to save all at once.

Change this:

    try
    {
        var id = new OrderDetailsDao().Insert(detail);
        var cart = (List<CartItem>)Session[CartSession];
        var orderDao = new OrderDao();
        decimal total = 0;
        foreach (var item in cart)
        {
            var order = new order();
            order.ProductId= item.Product.ProductId;
            order.IDOrder = id;
            order.Price = item.Product.Price;
            order.quantity = item.quantity;
            orderDao.Insert(order);
        }
    }

to:

    try
    {
        var id = new OrderDetailsDao().Insert(detail);
        var cart = (List<CartItem>)Session[CartSession];
        var orderDao = new OrderDao();
        decimal total = 0;
        List<order> orders = new List<order>();
        foreach (var item in cart)
        {        
            var order = new order();            
            order.ProductId= item.Product.ProductId;
            order.IDOrder = id;
            order.Price = item.Product.Price;
            order.quantity = item.quantity;
            orders.Add(order);
        }
        orderDao.Insert(orders);
    }

Also, change this:

  public bool Insert(order order)
  {
    try
    {
        re.order.Add(order);
        re.SaveChanges();
        return true;
    }
    catch
    {
        return false;
    }
}

to:

public bool Insert(List<order> orders)
{
    foreach(var order in orders)
    {
         re.order.Add(order);
    }  
    try
    {               
        re.SaveChanges();
        return true;
    }
    catch
    {
        return false;
    }
}

Upvotes: 1

Related Questions