I am trying to remove items that are created more then 1 day old, but when I click delete to execute Button3_Click event, only 1 item is delete hence I have to click the button multiple times if I want to delete all items that are 1 day old. Please kindly take a look, not sure which part went wrong Thanks:
protected void Button3_Click(object sender, EventArgs e)
{
using (var db = new CommerceEntities())
{
DateTime checkexpiryDate = DateTime.Now.AddDays(-1);
var itemstoDelete = from c in db.ShoppingCarts
where c.DateCreated < checkexpiryDate
select new
{
c.CartID,
c.ProductID,
c.Color,
c.Size,
};
foreach (var item in itemstoDelete)
{
try
{ //remove item code....
//RemoveItem(string cartID, int productID, string color, string size)
}
public void RemoveItem(string cartID, int productID, string color, string size) {
using (var db = new CommerceEntities())
{
....
..
//db.DeleteObject(myItem);
....
..
//db.SaveChanges()
...
....
..
}
}