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 March 29th, 2011, 12:51 AM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deleting using ListView

Imar,

This book is great! I worked through it completely and am now going back and working through certain sections again, particularly concepts that I have never worked with before such as LINQ and the ListView control.

The Delete button, referenced on page 457, removes the item from the Picture table which is the source table for the web page. However, it does not remove the actual file from the virtual folder. Is there a way to do this within the ListView control? Also, how could an album be deleted? Of course, this would require removing the name from the PhotoAlbum, removing the associated data rows in the Picture table and removing the actual files from the virtual folder.

Below is my code that correctly removes an album. Is there another way to do this? As you can tell, I'm working in VB.NET

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim photoAlbumId As Integer = Convert.ToInt32(DropDownList1.SelectedValue)
Dim myDatabaseContext As New SpaethFamilyDataContext
Dim pics = From p In myDatabaseContext.Pictures _
Where p.PhotoAlbum.Id = photoAlbumId _
Select p
For Each selectedDetail As Picture In pics
System.IO.File.Delete(Server.MapPath(selectedDetai l.ImageUrl))
myDatabaseContext.Pictures.DeleteOnSubmit(selected Detail)
Next
myDatabaseContext.SubmitChanges()
Dim Albm = From p In myDatabaseContext.PhotoAlbums _
Where p.Id = photoAlbumId _
Select p
myDatabaseContext.PhotoAlbums.DeleteAllOnSubmit(Al bm)
myDatabaseContext.SubmitChanges()
End Sub
End Class
 
Old March 29th, 2011, 02:49 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 there,

Your code looks fine, although you can do it slightly more efficient by querying the Album and the Pictures in one fell swoop:

Code:
 
Dim photoAlbumId As Integer = Convert.ToInt32(DropDownList1.SelectedValue)
Using myDatabaseContext As New PlanetWroxDataContext
  Dim album As PhotoAlbum = (From p In myDatabaseContext.PhotoAlbums _
                     Where p.Id = photoAlbumId _
                      Select p).FirstOrDefault()
  If album IsNot Nothing Then
    For Each pic As Picture In album.Pictures
      System.IO.File.Delete(Server.MapPath(pic.ImageUrl))
      myDatabaseContext.Pictures.DeleteOnSubmit(pic)
    Next
  myDatabaseContext.PhotoAlbums.DeleteOnSubmit(album)
  myDatabaseContext.SubmitChanges()
  End If
End Using
I also introduced a Using statement to automatically clean up the PlanetWrox data context when you no longer need it.

You can delete files with the ListView using its ItemDeleting event. Inside that event, you can construct a DataContext, delete the picture from disk and save the changes. This article may help in understanding what to do: http://www.4guysfromrolla.com/articles/052709-1.aspx

Hope this helps.

Cheers,

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; March 29th, 2011 at 04:12 AM..
 
Old March 29th, 2011, 04:03 AM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks for the quick reply

Imar,

The code you provided makes perfect sense.

I didn't find any events on the ListView control. Should I be looking for a delete event on the LinqDataSource?

Thanks again.
 
Old March 29th, 2011, 04:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Click the control in Design View and then look at the Events tab of the Properties Grid. It should be there:

http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx

Cheers,

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
listview styling robbaralla BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 8 February 22nd, 2010 06:54 AM
ListView fredforfree ASP.NET 2.0 Basics 1 August 1st, 2009 03:01 PM
ListView asters VB.NET 2002/2003 Basics 0 March 27th, 2008 05:00 AM
listView AristotleYu ASP.NET 2.0 Basics 0 May 11th, 2006 04:54 AM
Listview Sorting adroc Pro VB 6 0 June 11th, 2003 01:26 AM





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