Problem: Deleting with chk in DataList

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Problem: Deleting with chk in DataList

 
0
  #1
Oct 23rd, 2007
Can someone please help me figure out what I am doing wrong here? It is supposed to find the checkboxes per dataitem and see if that specific dataitem needs to be deleted. And if it does, to delete them all after adding it to one SQL statement. Please look below and give me insight!
  1. Sub btnDelete_click(ByVal sender As Object, ByVal e As System.EventArgs)
  2. Dim DeleteMe As Boolean
  3. Dim anItem As DataListItem
  4. Dim i As Integer = 0
  5. Dim strDeleted As String = "('"
  6. Dim SQLString As String = "DELETE FROM Photos WHERE PhotoID IN "
  7. For Each anItem In dlAlbumPhotos.Items
  8. DeleteMe = CType(anItem.FindControl("chkDelete"), CheckBox).Checked
  9. If DeleteMe Then
  10. i += 1
  11. if i = 1 then
  12. SQLString &= "('" & anItem.ItemIndex
  13. strDeleted &= anItem.ItemIndex
  14. else
  15. SQLString &= "', '" & anItem.ItemIndex
  16. strDeleted &= "', '" & anItem.ItemIndex
  17. end if
  18. Dim strDelName As String = anItem.ItemIndex
  19. Dim filepath As String = Server.MapPath("\") & "\Vegas2\Galleries\NL" & MakeInt(Trim(Request.QueryString("album"))) & "\"
  20. if File.Exists(filePath & strDelName & ".jpg") then file.delete(filePath & strDelName & ".jpg")
  21. if File.Exists(filePath & "T_" & strDelName & ".jpg") then file.delete(filePath & "T_" & strDelName & ".jpg")
  22. End if
  23. Next
  24. dlAlbumPhotos.DataBind()
  25. strDeleted &= "')"
  26. SQLString &= "')"
  27. if i > 0 then
  28. Dim conDelete As OdbcConnection
  29. Dim cmdSelect As OdbcCommand
  30. conDelete = New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
  31. cmdSelect = New OdbcCommand( SQLString, conDelete )
  32. conDelete.Open()
  33. cmdSelect.ExecuteNonQuery()
  34. conDelete.Close()
  35. lblDeletion.Visible = True
  36. lblDeletion.Text = "<br />Successfully deleted pictures " & strDeleted & "."
  37. end if
  38. End Sub
  39.  
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem: Deleting with chk in DataList

 
0
  #2
Oct 23rd, 2007
I did change the code, but for some reason my checkbox ALWAYS returns unchecked even though my controls are checked! Let me know if you happen to have a fix for this?

New code below:
  1. Sub btnDelete_click(sender As Object, e As System.EventArgs)
  2. Dim DLIC As DataListItemCollection = dlAlbumPhotos.Items
  3. Dim strPhotoIDs As String = ""
  4. Dim chkBox As Boolean = False
  5. Dim strDeleted As String = ""
  6. For Each Item As DataListItem In DLIC
  7. Dim DelCheckBox As CheckBox = CType(Item.FindControl("chkDelete"), CheckBox)
  8. If DelCheckBox.Checked Then
  9. chkBox = True
  10. strDeleted &= CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString & ","
  11. strPhotoIDs &= CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString & ","
  12. Dim strDelName As String = CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString
  13. Dim filepath As String = Server.MapPath("\") & "\Vegas2\Galleries\NL" & MakeInt(Trim(Request.QueryString("album"))) & "\"
  14. if File.Exists(filePath & strDelName & ".jpg") then file.delete(filePath & strDelName & ".jpg")
  15. if File.Exists(filePath & "T_" & strDelName & ".jpg") then file.delete(filePath & "T_" & strDelName & ".jpg")
  16. End if
  17. Next
  18. if chkBox then
  19. Dim conDelete As OdbcConnection
  20. Dim cmdSelect As OdbcCommand
  21. conDelete = New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
  22. Dim SQLString As String = "DELETE FROM Photos WHERE PhotoID IN (" & strPhotoIDs.Substring(0, strPhotoIDs.LastIndexOf(",")) & ")"
  23. cmdSelect = New OdbcCommand( SQLString, conDelete )
  24. conDelete.Open()
  25. cmdSelect.ExecuteNonQuery()
  26. conDelete.Close()
  27. lblDeletion.Visible = True
  28. lblDeletion.Text = "<br />Successfully deleted pictures (" & strDeleted.Substring(0, strDeleted.LastIndexOf(",")) & ")."
  29. end if
  30. End Sub
  31.  
Last edited by SheSaidImaPregy; Oct 23rd, 2007 at 8:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem: Deleting with chk in DataList

 
0
  #3
Oct 25th, 2007
Need help on this one.. thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem: Deleting with chk in DataList

 
0
  #4
Oct 25th, 2007
No matter what I do, it always comes up false. Is it due to the fact that I am using an OnClick event with NO behind coding?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem: Deleting with chk in DataList

 
0
  #5
Oct 25th, 2007
Okay, figured it out. It is quite odd, but I had to put all my entire Page_load event in the "if Not Page.IsPostBack then" statement. To me, this is quite odd as I wanted all the information to be updated whether it was posted back or not. But this is the only way that the checkboxes were not permanantly set to false during runtime.

Thanks though..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC