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!

Sub btnDelete_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DeleteMe As Boolean
Dim anItem As DataListItem
Dim i As Integer = 0
Dim strDeleted As String = "('"
Dim SQLString As String = "DELETE FROM Photos WHERE PhotoID IN "
For Each anItem In dlAlbumPhotos.Items
  DeleteMe = CType(anItem.FindControl("chkDelete"), CheckBox).Checked
  If DeleteMe Then
    i += 1
    if i = 1 then
      SQLString &= "('" & anItem.ItemIndex
      strDeleted &= anItem.ItemIndex
    else
      SQLString &= "', '" & anItem.ItemIndex
      strDeleted &= "', '" & anItem.ItemIndex
    end if
    Dim strDelName As String = anItem.ItemIndex
    Dim filepath As String = Server.MapPath("\") & "\Vegas2\Galleries\NL" & MakeInt(Trim(Request.QueryString("album"))) & "\"
    if File.Exists(filePath & strDelName & ".jpg") then file.delete(filePath & strDelName & ".jpg")
    if File.Exists(filePath & "T_" & strDelName & ".jpg") then file.delete(filePath & "T_" & strDelName & ".jpg")
  End if			
Next
dlAlbumPhotos.DataBind()
strDeleted &= "')"
SQLString &= "')"
if i > 0 then
  Dim conDelete As OdbcConnection
  Dim cmdSelect As OdbcCommand
  conDelete = New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
  cmdSelect = New OdbcCommand( SQLString, conDelete )
  conDelete.Open()
  cmdSelect.ExecuteNonQuery()
  conDelete.Close()
  lblDeletion.Visible = True
  lblDeletion.Text = "<br />Successfully deleted pictures " & strDeleted & "."
end if
End Sub

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:

Sub btnDelete_click(sender As Object, e As System.EventArgs)
  Dim DLIC As DataListItemCollection = dlAlbumPhotos.Items
  Dim strPhotoIDs As String = ""
  Dim chkBox As Boolean = False
  Dim strDeleted As String = ""
  For Each Item As DataListItem In DLIC
    Dim DelCheckBox As CheckBox = CType(Item.FindControl("chkDelete"), CheckBox)
    If DelCheckBox.Checked Then
      chkBox = True
      strDeleted &= CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString & ","
      strPhotoIDs &= CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString & ","
      Dim strDelName As String = CType(dlAlbumPhotos.FindControl("PhotoID"), Label).Text.ToString
      Dim filepath As String = Server.MapPath("\") & "\Vegas2\Galleries\NL" & MakeInt(Trim(Request.QueryString("album"))) & "\"
      if File.Exists(filePath & strDelName & ".jpg") then file.delete(filePath & strDelName & ".jpg")
      if File.Exists(filePath & "T_" & strDelName & ".jpg") then file.delete(filePath & "T_" & strDelName & ".jpg")
    End if			
  Next
  if chkBox then
    Dim conDelete As OdbcConnection
    Dim cmdSelect As OdbcCommand
    conDelete = New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
    Dim SQLString As String = "DELETE FROM Photos WHERE PhotoID IN (" & strPhotoIDs.Substring(0, strPhotoIDs.LastIndexOf(",")) & ")"
    cmdSelect = New OdbcCommand( SQLString, conDelete )
    conDelete.Open()
    cmdSelect.ExecuteNonQuery()
    conDelete.Close()
    lblDeletion.Visible = True
    lblDeletion.Text = "<br />Successfully deleted pictures (" & strDeleted.Substring(0, strDeleted.LastIndexOf(",")) & ")."
  end if
End Sub

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?

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..

This question has already been answered. Start a new discussion instead.