473,480 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem deleting multiple rows - Nearly there, PLEASE HELP

66 New Member
Hi all,

im nearly there with this one and im sure it shouldnt be hard to solve, i just cant seem to find the solution.

ive got records being displayed, the user can then tick what records to delete... click delete.. it shows deleted records then you can go back to the screen, It works if one record is selected but any more it doesnt delete any so im sure there is a problem with my SQL statement.

the code for the 2 pages have been shown below:

The code for the selection page
=======================
Expand|Select|Wrap|Line Numbers
  1.       <div align="center">
  2.         <%
  3. If rssearchcnx.BOF and rssearchcnx.EOF Then%> 
  4.     We appolagise, but no flights were found matching your search
  5.       </div>
  6.       <p align="center"><a href="search.asp" target="mainFrame">please try again</a></p>
  7. <%Else%>
  8.  
  9. <%If Not rssearchcnx.BOF Then%>
  10. <%
  11.     Do While Not rssearchcnx.EOF
  12.     %>
  13. <tr>
  14.         <td ><%=rssearchcnx("flight_id")%></td>
  15.         <td></td>
  16.         <td>
  17.         <form name="form" method="post" action="delete.asp">
  18.         <input type="checkbox" name="flight_id" value="<%=(rssearchcnx.Fields.Item("flight_id").Value)%>"></td>
  19.  
  20.       </tr>
  21.     <% rssearchcnx.MoveNext
  22.     Loop
  23.     %>
  24.  
  25. </table>
  26.  
  27. <%End If%>
  28. <%End If%>
  29.     <input type="submit" value="Delete Selected Records">
  30. </form>
  31. <%
  32. rssearchcnx.Close
  33. searchcnx.Close
  34. %>
  35.  
code for the delete action page
======================
Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. Dim flight_id
  4.  
  5. flight_id = Request.Form("flight_id")
  6.  
  7.  
  8. SQL="DELETE * FROM tbl_flight_details WHERE flight_id ='"+Request.Form("flight_id")+"'"
  9.  
  10.  Set MyConn=Server.CreateObject("ADODB.Connection")
  11. MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\ddwassignment3\airlines.mdb"
  12.  
  13. Set MyRS=Server.CreateObject("ADODB.RecordSet")
  14. MyRS.Open SQL,MyConn
  15.  
  16. %>
  17.  
  18. </p>
  19.       <table width="37%" border="0" align="center">
  20.         <tr>
  21. <td align="center"><% Response.Write("<font face=Calibri>You have deleted flight number <B><font face=Calibri color=Red>" & flight_id)%>
  22.  
any help will be MUCH appreciated

kind regards

jason
Mar 10 '08 #1
5 1882
jeffstl
432 Recognized Expert Contributor
You have to execute the DELETE SQL in a loop (more then once) to delete multiple records.

You will need a check that gathers all the id's checked by the user into an array.

The you will need to loop through that array and run the SQL delete statement for EACH id in the array.

With this current set up I am not sure which direction to tell you. I would probably instead of letting them do check boxes and submitting a whole bunch to process, you could set it up where they can delete one at a time quicker by redirecting them to the page displaying the records.

Print this link out next to each record:
Expand|Select|Wrap|Line Numbers
  1. 'FORM PAGE
  2. response.write <a href=delete.asp?id=" & rssearchcnx.Fields.Item("flight_id").Value & ">Delete</a>"
  3.  
  4. 'THEN ON YOUR DELETE.ASP
  5. 'take the id from the querystring instead
  6. <%
  7.  
  8. Dim flight_id
  9.  
  10. flight_id = Request.Querystring("id")
  11.  
  12.  
  13. SQL="DELETE * FROM tbl_flight_details WHERE flight_id ='"+Request.Form("flight_id")+"'"
  14.  
  15.  Set MyConn=Server.CreateObject("ADODB.Connection")
  16. MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\ddwassignment3\airlines.  mdb"
  17.  
  18. 'execute sql string!!
  19. MyConn.execute SQL
  20.  
  21. 'then just redirect back to your page listing all the records and delete links!
  22. response.redirect("formpage.asp")
  23.  
  24. 'dont really need to create a record set to execute a DELETE, only when ur 'SELECTING records I think
  25. 'Set MyRS=Server.CreateObject("ADODB.RecordSet")
  26. 'MyRS.Open SQL,MyConn
  27.  
  28. %>
  29.  
  30.  
Mar 10 '08 #2
jasone
66 New Member
Hi thanks for your response, a little unsure where to put this data, is it possible for you to insert the script into the script i have already posted, and then post it back on here?

many many thanks

jason
Mar 10 '08 #3
jasone
66 New Member
i forgot to say im using checkboxes.. so there is no need for the delete text next to each record
Mar 10 '08 #4
jeffstl
432 Recognized Expert Contributor
All I can say like stated before is that in order to use checkboxes and process many deletes at once, you will need to set up and pass an array of "id" numbers to loop through.

Im not sure how many records you are dealing with when you print out the area for them to select the checkboxes, so Im not sure what the best solution is, Its not simple to determine how many checkboxes are checked on a page when there could be 5 or there could be a 1000.

Just off hand on your delete.asp page you will need something like this where MyCheckBox is your array of checkboxes:

Again to avoid all this, you could use my example where there is a delete link next to each record and they click it and it goes right back to the page they were just viewing with the record deleted.

Expand|Select|Wrap|Line Numbers
  1.  
  2. for FlightID=0 to MyRS.RecordCount
  3.      if MyCheckBox(FlightID).Value = 1 then
  4.           'execute delete SQL here using FlightID
  5.      end if
  6.  
  7. Next
  8.  
  9.  
Mar 10 '08 #5
jhardman
3,406 Recognized Expert Specialist
Hey Jasone,

If Jeff's instructions were a jump for you, try a multi-step approach like this.

1- as it is you are opening up a new form for each row, only one form is going to be sent to the next page, so make sure the <form> tag is before the loop,

2- open up the data posted from your form to make sure it is in a good format:
Expand|Select|Wrap|Line Numbers
  1. for each x in request.form
  2.    response.write x & ": " & request.form(x) & "<br>" & vbNewLine
  3. next
If all of the flight_id numbers had the same input name (name="flight_id") then you should get a list in response like this:
Expand|Select|Wrap|Line Numbers
  1. flight_id: 23, 75, 63, 192
Make sure these are all the records you want to delete before you go on.

3- Open the db and SELECT these records. Make sure you are getting the right records. I actually recommend you put up a confirmation dialogue before you delete, but regardless, I would open with a SELECT statement first.
Expand|Select|Wrap|Line Numbers
  1. query = "SELECT * FROM tbl_flight_details WHERE flight_id IN " & Request.Form("flight_id")
Notice the "IN" keyword allows you to select values from a list, otherwise, you are saying you want the flight_id to equal 23,75,63,192 and there is no flight id that equals that. Look at the list this pulls up to make sure it is pullingup the right records (as I mentioned before, I usually leave this step in the final version as a confirmation "Are you sure you want to delete these recdords?")
Expand|Select|Wrap|Line Numbers
  1. response.write "<table><tr>"
  2. for each x in objRS.fields
  3.    response.write "<th>" & x.name & "</th>"
  4. next
  5. response.write "</tr>" & vbNewLine
  6.  
  7. do until objRS.eof
  8.    response.write "<tr>"
  9.  
  10.    for each x in objRS.fields
  11.       response.write "<td>" & x.value & "</td>"
  12.    next
  13.  
  14.    response.write "</tr>"
  15.    objRS.moveNext
  16. loop
  17.  
  18. Response.write "</table>"
  19.  
4- if this looks good, then to delete you just need to change the word "SELECT" to "DELETE" and it will go.

Let me know if this helps.

Jared
Mar 12 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
14714
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
7
9760
by: Jon Maz | last post by:
Hi, I have a MySql problem I hope someone can help me with. I'm trying to run an update on a linking table, the update is running into a Primary Key constraint violation, and in my workaround...
2
1637
by: Brad Pears | last post by:
I am working on a new vb.net 2005 project using SQL server 2000 as the backend db. I have a listview where control I want a user to be able to select either just one or multiple rows in the...
3
3900
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.