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()               
...                
....             
   ..            
 }
}

Hi, yes, I want to delete the entire row off for all items that are older then 1 day in 1 click. Right now it is deleting 1 row with each click.

Then you need some sql query, like:

SqlConnection sqlConn = new SqlConnection("connString");
SqlCommand cmd = new SqlCommand();
stirng query = @"DELETE FROM MyTable WHERE MyDateColumn < @myDate";
cmd.Parameters.Add("@myDate", SqlDbType.DateTime).Value = DateTime.Now.AddDays(-1);
This article has been dead for over six months. Start a new discussion instead.