944,068 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 1441
  • ASP.NET RSS
Oct 23rd, 2007
0

Problem: Deleting with chk in DataList

Expand Post »
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!
ASP.NET Syntax (Toggle Plain Text)
  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.  
Similar Threads
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Oct 23rd, 2007
0

Re: Problem: Deleting with chk in DataList

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:
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Oct 25th, 2007
0

Re: Problem: Deleting with chk in DataList

Need help on this one.. thanks
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Oct 25th, 2007
0

Re: Problem: Deleting with chk in DataList

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?
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Oct 25th, 2007
0

Re: Problem: Deleting with chk in DataList

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..
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: how to develop online test
Next Thread in ASP.NET Forum Timeline: Help me in username and password validation through accessing the database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC