Reputation: 27
I have 2 tables:
Table order:
IDorder, productId, quantity, price
Table orderdetails:
ID, ShipName, ShipMobile, ShipAddress, ShipEmail, DateOrder.
My OrderDetailsDao (As Model):
raovatmuabanthucungEntities re = null;
public OrderDetailsDao()
{
re = new raovatmuabanthucungEntities();
}
public long Insert(orderdetails detail)
{
re.orderdetails.Add(order);
re.SaveChanges();
return order.ID;
}
My OrderDao:
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");
}
var cart = (List<CartItem>)Session[CartSession];
is my session to save products added to cart. Sorry for disturbing you but i don't know how to fix this. Please tell me if i miss something, i will update my question ... Thank for all. Upvotes: 1
Views: 103
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