Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2013, 08:45 PM
Authorized User
 
Join Date: Aug 2013
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
Default Deletion of Albums using a button

I want to write code inside a button's Click event that will delete all the photo albums that have been on the site for more that 3 months from the CreateDateTime of the the album, and delete all the related pictures in the GigPics folder.

I was thinking of using a loop to go over all the records but didn't really know how to write such code?? Any help is appreciated and this is my last question.. After that my classified ads site is done and i really thank u Imar for such a GREAT book :)
 
Old August 25th, 2013, 06:48 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Mark,

I haven't fully tested it yet, but the following should work:

Code:
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
  DateTime oldDate = DateTime.Now.AddMonths(-3);
  var allOldAlbums = (from a in myEntities.PhotoAlbums
                      where a.CreateDate < oldDate
                      select a).ToList();

  foreach (var album in allOldAlbums)
  {
    foreach (var picture in album.Pictures.ToList())
    {
      File.Delete(Server.MapPath(picture.ImageUrl));
      myEntities.Pictures.DeleteObject(picture);
    }
    myEntities.PhotoAlbums.DeleteObject(album);
  }
  myEntities.SaveChanges();
}
Most of it is hopefully straight forward. For an explanation of why you need the two calls to ToList, check out this SO post: http://stackoverflow.com/questions/3...n-foreach-loop

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 - Inserted photo albums not displaying jrwhite BOOK: Beginning ASP.NET 4 : in C# and VB 0 September 3rd, 2012 02:30 PM
Only admin be able to create albums for users sophia BOOK: Beginning ASP.NET 4 : in C# and VB 9 August 13th, 2012 01:52 AM
One img at 2 different albums Oshrib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 May 8th, 2011 03:00 PM
All Photo Albums page cbspira BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 January 10th, 2009 08:09 AM
Deletion.. ankur_icfai SQL Server 2000 8 August 30th, 2006 07:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.