Cascade Record & Image Deletes

This tutorial is the third of a three part series on image delete options. Unlike the other two tutorials we will be not working with George Petrov's Pure ASP upload behavior on this page. Instead we will look at how to remove related records and the images associated with them when deleting a master record. By controlling orphaned records we keep the database tables clean and our applications run faster with fewer resources consumed. It only makes sense to remove images associated with the deleted records as well, again conserving server resources.

Creating the Recordsets

We need to create three recordsets on the delete page. One recordset—rs_versions—will be conditional to keep the page from crashing when deleting categories without products. We will also need to custom code the SQL query to use the rs_products("P_ID") value to select the records from the versions table marked for deletion.

The first recordset we’ll create is rs_categories. The only parameter we need to pass from our recordset navigation page to the delete record page is the Category_ID value. This will be the filter. If your navigation page uses links to select records for deletion, then the filter will be a URL querystring. If you use a drop down menu it will be a form variable. The rs_products recordset will use the same filter. For reference see the UD generated recordsets below:

<%
Dim rs_categories__MMColParam
rs_categories__MMColParam = "1"
if (Request.QueryString("Category_ID") <> "") then rs_categories__MMColParam = Request.QueryString("Category_ID")
%>
<%
set rs_categories = Server.CreateObject("ADODB.Recordset")
rs_categories.ActiveConnection = MM_yourdatasource_STRING
rs_categories.Source = "SELECT * FROM Product_Categories WHERE Category_ID = " + Replace(rs_categories__MMColParam, "'", "''") + ""
rs_categories.CursorType = 0
rs_categories.CursorLocation = 2
rs_categories.LockType = 3
rs_categories.Open()
rs_categories_numRows = 0
%>
<%
Dim rs_products__MMColParam
rs_products__MMColParam = "1"
if (Request.QueryString("Category_ID") <> "") then rs_products__MMColParam = Request.QueryString("Category_ID")
%>
<%
set rs_products = Server.CreateObject("ADODB.Recordset")
rs_products.ActiveConnection = MM_yourdatasource_STRING
rs_products.Source = "SELECT * FROM Products WHERE Category_ID = " + Replace(rs_products__MMColParam, "'", "''") + ""
rs_products.CursorType = 0
rs_products.CursorLocation = 2
rs_products.LockType = 3
rs_products.Open()
rs_products_numRows = 0
%>

Nothing fancy there, but it gets interesting now. Create the recordset rs_versions in the data bindings window. Don’t bother to set any filter we will do that in code view. After you’ve created rs_versions, open the Code Inspector. Notice the comment I’ve added to the code block and the following conditional statement. If rs_products is empty rs_versions is not created for two reasons. First the page will crash if it tries to create rs_versions without a rs_products(“P_ID”) value. And second there are no version images to delete because there are no versions. The next code change is in the SQL query statement. We add a “Where” directive and set the “Where” condition equal to the rs_products record ID number (“P_ID”). When you’re done, close the “if then” statement, save the file, and return to design view.

<%
‘set condition for versions recordset creation
If Not rs_products.eof then
set rs_versions = Server.CreateObject("ADODB.Recordset")
rs_versions.ActiveConnection = MM_yourdatasource_STRING
‘set versions recordset equal to product id
rs_versions.Source = "SELECT * FROM Versions WHERE P_ID =" & rs_products("P_ID")
rs_versions.CursorType = 0
rs_versions.CursorLocation = 2
rs_versions.LockType = 3
rs_versions.Open()
rs_versions_numRows = 0
end if
%>

When you get back to design view the first thing you should notice is that the rs_versions recordset has vanished from the data bindings window. Don’t panic—it’s supposed to do this. Go ahead and add the delete records server behavior to the page for rs_categories.

Okay, now that we have the recordsets we need to get all the image names for deletion. To accomplish this we need a repeat region to loop through the Products table and one to loop through the Versions table and collect image values for the delete. You can use any method to create two repeat regions that works for you. I pull a field value from the Products table and apply the repeat region behavior selecting show all records. I then return to code view, copy and paste a copy of the repeat region and change the variable names as shown. You could also drag field elements from the Products and Versions onto the page before modifying rs_versions and cut and paste the resulting code. Regardless of method you need two repeat regions on the page one for rs_products and one for rs_versions.

<%
Dim Repeat1__numRows
Repeat1__numRows = -1
Dim Repeat1__index
Repeat1__index = 0
rs_products_numRows = rs_versions_numRows + Repeat1__numRows
%>
<%
'repeat behavior modified  for nesting
Dim VRepeat1__numRows
VRepeat1__numRows = -1
Dim VRepeat1__index
VRepeat1__index = 0
rs_versions_numRows = rs_versions_numRows + VRepeat1__numRows
%>

Go back Download Tutorial PDF Next: The Delete Code

James Threadgill

James ThreadgillJames Threadgill has authored numerous tutorials on ASP and ASP.NET web development, published on such sites as the Dynamic Zones and MSDN Accademic Alliance. He co-authored the Sam's book Dreamweaver MX: ASP.NET Web Development.

James first began computer programming in 1995 while attending Alvin Community College. He completed a certificate of computer science program and an Associate of Arts degree at Alvin before going on to the University of Houston-Clear Lake where he was awarded a Bachelor of Science and a Master of Arts.

James publishes fiction, poetry, and visual arts under the name Wayne James. His fiction first appeared in Raconteur in 1995 and since has been published numerous times: in Hadrosaur Tales 5 and 7, Bayousphere, and in the Write Gallery e-zine. His poetry first appeared in the small press magazine Lucidity in 1996 and has been published numerous times since. His collection of fiction and poetry, When Only the Moon Rages, was released in 2000. Most recently his work appeared in Tales of the Talisman winter 2010 and spring 2011 issues. James currently attends graduate school at the University of Houston and owns and operates small web design and internet marketing firm, WWWeb Concepts, with his wife, Karen, in Houston, TX USA.

See All Postings From James Threadgill >>

Comments

DMX 2004 and the Cascade delete

June 10, 2004 by Kenny Darcy
Hello,
Firstly thank you for your tutorials, I am very grateful. I am going to give the Cascade Record and Images deletes a go and was wondering, using mx2004 has things changed any to make it a little easier to do this.

Regards

Kenny



RE: DMX 2004 and the Cascade delete

February 18, 2006 by James Threadgill

You are welcome, Kenny. I was quite ill when you posted, but I'm back now!

Cascading file deletes are pretty complex considering the loops required. I wouldn't expect DMX to make this sort of thing point and click anytime soon.

RE: RE: DMX 2004 and the Cascade delete

February 18, 2006 by Kenny Darcy
Ah ha! Thanks for the reply Wayne, and great to hear your  better. I did do your tutorial back then and it all worked fine.

re " I wouldn't expect DMX to make this sort of thing point and click anytime soon."

I have found the MX Kollection from Interakt does just that.

regards

Kenny

You must me logged in to write a comment.