Home > SQL Development > SQL deleting a record problem

SQL deleting a record problem



Hi All



I have got one here i can't work out. I am trying to add a manage links section to a links page where the links are sourced from a access database.



The basic table contains a link title field, a link field a description field and a link position field. the link position field controls the actual position in the links page and is just sequential numbers with 1 being the first link 2 being the second etc.



I can edit the links OK using the code i have created, even change the position of the links in the list. the problem occurs when i try to delete a link.



The actual link delete works, its the code that recalculates the link position field that seems to be causing the problem.




Code:


	' 	*** if Delete then delete and Update all Link Positions
if request.form("chkDelete") = "ON" Then
' *** Delete record
StrSql = "DELETE FROM tblLinks WHERE LinkTitle = '" & lnkTitle & "'"
adoCon.execute (StrSql)
' *** Recalculate all positions
StrSql = "UPDATE tblLinks SET LinkPos=LinkPos-1 WHERE LinkPos >= " & hdnPosition
adoCon.execute (StrSql)
end if






The problem i am seeing is when i delete link 3 say it actually deletes link 4, if i comment out the code that recalculates the linkPos, it deletes the correct link but obviously there is a gap in the linkPos sequence.



I have tried replacing the recalculate code with a loop that updates the records individually but this has the same effect.




Code:


	' 	*** if Delete then delete and Update all Link Positions
if request.form("chkDelete") = "ON" Then
' *** Delete record
StrSql = "DELETE FROM tblLinks WHERE LinkTitle = '" & lnkTitle & "'"
adoCon.execute (StrSql)
' *** Recalculate all positions
strSQL = "SELECT * FROM tblLinks ORDER BY LinkPos ASC"
rsLinks.Open strSQL, adoCon
varPos = 1
do While NOT rsLinks.EOF
if rsLinks("LinkPos") = varPos then

Else
StrSql = "UPDATE tblLinks SET LinkPos=" & varPos & " WHERE ID = " & rsLinks("ID")
adoCon.execute (StrSql)
End if
rsLinks.movenext
varPos = varPos + 1
Loop
rsLinks.Close
end if






Strange thing is i use basically the same code to move the link up and down the list. in the following code block you can see the code following the delete code.




Code:


if Request.querystring("function") = "update" then
lnkPos = Request.Form("valPos")
hdnPosition = Request.Form("hdnPos")
lnkCat = Request.Form("txtCategory")
lnkTitle = Request.Form("txtTitle")
lnkLink = Request.Form("txtLink")
lnkDesc = Request.Form("txtDesc")
lnkPhone = Request.Form("txtPhone")

' *** if Delete then delete and Update all Link Positions
if request.form("chkDelete") = "ON" Then
' *** Delete record
StrSql = "DELETE FROM tblLinks WHERE LinkTitle = '" & lnkTitle & "'"
adoCon.execute (StrSql)
' *** Recalculate all positions
strSQL = "SELECT * FROM tblLinks ORDER BY LinkPos ASC"
rsLinks.Open strSQL, adoCon
varPos = 1
do While NOT rsLinks.EOF
if rsLinks("LinkPos") = varPos then

Else
StrSql = "UPDATE tblLinks SET LinkPos=" & varPos & " WHERE ID = " & rsLinks("ID")
adoCon.execute (StrSql)
End if
rsLinks.movenext
varPos = varPos + 1
Loop
'StrSql = "UPDATE tblLinks SET LinkPos=LinkPos-1 WHERE LinkPos >= " & hdnPosition
'adoCon.execute (StrSql)
rsLinks.Close
end if
' *** if position change Update all Link Positions
if lnkPos <> hdnPosition Then
' *** Delete current version of record
StrSql = "DELETE FROM tblLinks WHERE LinkPos = " & hdnPosition
adoCon.execute (StrSql)
' *** Recalculate all positions
StrSql = "UPDATE tblLinks SET LinkPos=LinkPos-1 WHERE LinkPos >= " & hdnPosition
adoCon.execute (StrSql)
' *** Re-Position allowing for new position of record
StrSql = "UPDATE tblLinks SET LinkPos=LinkPos+1 WHERE LinkPos >= " & lnkPos
adoCon.execute (StrSql)
' *** Add record back in
StrSql = "INSERT INTO tblLinks (LinkPos,Category,LinkTitle,Link,LinkDesc,LinkPhon e) VALUES (" & lnkPos & ",'" & lnkCat & "','" & lnkTitle & "','" & lnkLink & "','" & lnkDesc & "','" & lnkPhone & "')"
adoCon.Execute (StrSql)

Else
StrSql = "UPDATE tblLinks SET LinkPos=" & lnkPos & ", Category='" & lnkCat & "', LinkTitle='" & lnkTitle & "', Link='" & lnkLink & "', LinkDesc='" & lnkDesc & "', linkPhone='" & lnkPhone & "' WHERE LinkPos = " & lnkPos
'response.write strSQL
adoCon.execute (StrSql)
end if
End if








I am not sure if this is a problem with the SQL code or the ASP code, any suggestions would be gratefully received.



Regards

Ian

    
Guest


I wouldn't worry about gaps in the sequence.... They will still be in order

when you select them.



Also having sequential numbers can cause other issues.



What happens if you want to insert a link between 4 and 5?

Was this answer helpful ? Yes No   
Guest


I have just added the code to add a link, and i just use the "*** if position change Update all Link Positions" section and it seems to work. All i do is "UPDATE tblLinks SET LinkPos=LinkPos+1 WHERE LinkPos >= " & lnkPos" this allows you to insert links any where in the sequence.



I could try adding a seperate link to re-sequence the links, for myself i would have no problem with gaps in the sequence but the end user of the site will probably have issues seeing gaps in what they think should be a constant sequence.

Was this answer helpful ? Yes No   
Guest


End users - gotta love em



Have you tried outputting your update statement to the browser

just to make sure it says what you expect?

Was this answer helpful ? Yes No   
Guest


Problem solved.



I created a separate link on the page to re-sequence the links and that works, so i then tried a response.redirect out of the delete routine to the re-sequence code and it has solved the problem.



It must have something to do with in the original code the re-sequence was occurring directly after the deleting of the record, now with the response.redirect it delays it slightly or something giving it the chance to work.



Thanks for your suggestion though, it gave me another way of thinking enabling the solution.



Cheers.

Was this answer helpful ? Yes No   
Guest


No worries - glad it's fixed

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2008 by Infoqu. All rights reserved