| | |
Problem: Deleting with chk in DataList
Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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)
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
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
New code below:
ASP.NET Syntax (Toggle Plain Text)
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
Last edited by SheSaidImaPregy; Oct 23rd, 2007 at 8:43 pm.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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..
Thanks though..
![]() |
Similar Threads
- IE problem (Viruses, Spyware and other Nasties)
- Installation problem (Windows NT / 2000 / XP)
- Browser Hijacking - AAARGH! (Viruses, Spyware and other Nasties)
- Wierd Problem please help! (Windows NT / 2000 / XP)
- Deleting a record from a file (C)
- IE 6.0 Problem (Web Browsers)
- Sorry, another "bridge.dll" problem.. here's my HJT log. (Viruses, Spyware and other Nasties)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP)
- Do I have the WORM/VIRUS on my laptop? (Windows NT / 2000 / XP)
Other Threads in the ASP.NET Forum
- Previous Thread: how to develop online test
- Next Thread: Help me in username and password validation through accessing the database
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer c# c#gridviewcolumn checkbox class commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist development dgv dropdownlist dropdownmenu dynamic dynamically edit fileuploader fill findcontrol flash flv formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox menu microsoft mouse mssql multistepregistration nameisnotdeclared news objects opera panelmasterpagebuttoncontrols problem redirect registration relationaldatabases reportemail rotatepage schoolproject security serializesmo.table silverlight smartcard smoobjects software sql sql-server sqlserver2005 ssl textbox tracking unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webdevelopment webprogramming webservice youareanotmemberofthedebuggerusers






