Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 December 30th, 2008, 07:21 PM
Registered User
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 13 - Deleting pictures from GigPics directory

I noticed that deleting a picture from an album would delete the metadata for that pic from the database, but it would leave the file in the GigPics folder. I was thinking that something could be done in ListView1_ItemDeleting, but I am fairly new to this part of .NET and was struggling to figure out how to get the image url for the item being deleted so I could delete the file first and then the DB record. Also, wasn't sure if an industrial strength solution would need to be concerned with making sure the file and db record delete occurred as one atomic transaction.

Could someone please share a basic solution and at least thoughts on an industrial solution?

Thanks,

tinman
 
Old December 31st, 2008, 08:27 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi tinman,

The Deleting event is indeed a good place. The e parameter has a Keys property that gives you access to the Id of the picture being deleted. You can then use some LINQ to SQL to find the right picture based on its id, look at the ImageUrl of the property and use that to delete the image from disk. Something like this would work:

Code:
 
protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
  try
  {
    int pictureId = Convert.ToInt32(e.Keys[0]); // get the Id of the Picture being deleted
    using (PlanetWroxDataContext myContext = new PlanetWroxDataContext())
    {
      // Use LINQ to SQL to find the right picture
      Picture myPicture = (from p in myContext.Pictures
                           where p.Id == pictureId
                           select p).SingleOrDefault();
      if (myPicture != null)
      {
        // Found it; now try to delete it
        string imagePath = Server.MapPath(myPicture.ImageUrl);
        if (System.IO.File.Exists(imagePath))
        {
          System.IO.File.Delete(imagePath);
        }
      }
    }
  }
  catch (Exception ex)
  {
    // Ooops, something went wrong deleting the picture. Cancel delete from DB as well
    e.Cancel = true;  
  }
}
It's pretty difficult to do this as an atomic transaction as the file system isn't transactional. My implementation comes reasonably close. Only when deleting the images succeeds can the database delete be done.

It's probably better to do this in the Deleted event though. That way you can be sure that the record has been deleted from the database successfully before you attempt to delete the image. It's better to have an image without a database record than the other way around as you could end up with broken images if you can't delete the record from the database. You could check e.Exception in the Deleted event to see if the delete operation has succeeded or not.

This is as "industrial" as it gets without writing a lot of code. If you really need this to be transactional, you could also store the image in the database instead of on disk. That way, it's part of the DB record (or at least related to it) and gets deleted automatically. For pros, cons and a detailed code example, check here:

http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=414

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!

Last edited by Imar; December 31st, 2008 at 10:57 AM.. Reason: spelling
 
Old December 31st, 2008, 10:47 AM
Registered User
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks!

Imar,

Thanks for the quick and complete reply. I had been thinking that the whole object being deleted was available through the ListViewDeleteEventArgs, but all I could find was the id. So, it makes sense to go back to the DB to get the whole object.

Thanks again, and Happy New Year!

tinman
 
Old December 31st, 2008, 10:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. Happy New Year to you too...

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!
 
Old October 2nd, 2009, 02:25 PM
Authorized User
 
Join Date: Sep 2009
Posts: 23
Thanks: 9
Thanked 1 Time in 1 Post
Default

Imar,

I just wanted to say thanks, as your solution was benefical to me here as well. I'm a VB coder, so I've convereted your example into VB for anybody interested in the source for that language.

Code:
Protected Sub ListView1_ItemDeleting(ByVal sender As Object, ByVal e As _ System.Web.UI.WebControls.ListViewDeleteEventArgs) Handles ListView1.ItemDeleting
        Try
            ' Get the Id of the Picture being deleted
            Dim pictureId As Integer = Convert.ToInt32(e.Keys(0))

            Using myContext As New PlanetWroxDataContext
                ' Use LINQ to SQL to find the right picture
                Dim myPicture As Picture = (From p In myContext.Pictures _
                                                      Where p.Id = pictureId _
                                                      Select p).SingleOrDefault
                If myPicture IsNot Nothing Then
                    ' Found it; now try to delete it
                    Dim imagePath As String = Server.MapPath(myPicture.ImageUrl)
                    If System.IO.File.Exists(imagePath) Then
                        System.IO.File.Delete(imagePath)
                    End If
                End If
            End Using

        Catch ex As Exception
            ' Ooops, something went wrong deleting the picture. Cancel delete from DB
            e.Cancel = True
        End Try
    End Sub
Thanks,

John
The Following User Says Thank You to jsymons For This Useful Post:
Imar (October 4th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
display pictures from directory chetrity Classic ASP Basics 1 June 15th, 2006 01:01 PM
display pictures from directory chetrity Classic ASP Components 2 June 14th, 2006 01:20 AM
deleting images from a directory using php Adam H-W Beginning PHP 1 June 7th, 2006 08:57 AM
Chapter 13 ElMorenito BOOK: Beginning ASP 3.0 0 January 14th, 2005 02:56 PM





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