Results 1 to 9 of 9

Thread: ASP page with javascript to delete db

  1. #1
    Join Date
    Feb 2002
    Location
    Dallas, Texas
    Posts
    6

    Question ASP page with javascript to delete db

    This one is driving me nuts!! A simple ASP page named Admin_DocList.asp. Connection to database is OK. Requests documents from recordset and displays OK. Then "onClick" link (with appropriate underline and hand cursor) to delete document from recordset--starts javascript function that captures the DocumentID and navigates the window to trigger the If clause of the Query string that deletes the document. Everything works but the documents aren't deleted from the database. It's not a javascript error--it has to be in the piece that connects the javascript to the ASP (The "action"="del") but I'll be darned if I can find my error.
    Thanks,
    Mike


    <%
    dim strConn
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/Document.mdb")
    dim adoCn
    dim strSQL
    set adoCn = Server.CreateObject("ADODB.Connection")
    adoCn.Open strConn

    if Request.QueryString("action")="del" then
    strSQL = "DELETE FROM Documents where DocumentID=" & Request.QueryString("DocumentID")
    adoCn.Execute strSQL
    end if

    dim adoRs
    set adoRs = Server.CreateObject("ADODB.Recordset")
    strSQL = "Select * From Documents order by DocumentID Desc"
    set adoRs = adoCn.Execute(strSQL)
    %>

    <html>
    <body style="FONT-FAMILY: Verdana;FONT-SIZE: x-small;">

    <font size=3><b>My Documents</b></font>
    <font size=1>(<A HREF="admin_DocAdd.asp">Create New Document</A>)</font>

    <ul>
    <%
    Do While Not adoRs.EOF%>
    <li>
    <%=adoRs("Title")%>
    <br>
    <font size=1>
    <%=adoRs("DateCreated")%>&nbsp;&nbsp;
    [<a href="admin_DocEdit.asp?DocumentID=<%=adoRs("Docum entID")%>">edit</a>]
    [<span style="cursor:hand;TEXT-DECORATION: underline" onclick="DelDocument(<%=adoRs("DocumentID")%>);">d el</span>]
    </font>
    <br>&nbsp;
    </li>
    <%
    adoRs.MoveNext
    Loop%>
    </ul>

    <script language=JavaScript>
    function DelDocument(DocumentID)
    {
    if (confirm("Delete this document ?") == true)
    {
    window.navigate("admin_DocList.asp?action=del&Docu mentID="+DocumentID);
    }
    }
    </script>
    </body>
    </html>

    <%
    adoRs.Close
    set adoRs = nothing

    adoCn.Close
    set adoCn = nothing
    %>
    mike@texaswebdevelopers.com
    http://www.texaswebdevelopers.com

  2. #2
    Join Date
    Feb 2002
    Location
    Campbellsport, Wisconsin
    Posts
    1,989
    See if this works



    Code:
    dim strConn 
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/Document.mdb") 
    dim adoCn 
    dim strSQL 
    set adoCn = Server.CreateObject("ADODB.Connection") 
    adoCn.Open strConn 
    
    dim adoRs 
    set adoRs = Server.CreateObject("ADODB.Recordset") 
    strSQL = "Select * From Documents order by DocumentID Desc" 
    adoRs.Open strSQL, adoCn 
    
    if adoRs.Supports(16778240) then ' Updates
         if Request.QueryString("action")="del" then
    	dim myfilter
    	myfilter = "DocumentID=" & Request.QueryString("DocumentID")
    
    	adoRs.Filter = myfilter
    	adoRs.Delete
    	
    	if adoRs.EOF Then
    		adoRs.MovePrevoius
    	End If
         End If
    End If

  3. #3
    Join Date
    Feb 2002
    Location
    Dallas, Texas
    Posts
    6
    Thanks Bud,
    I'll try this in the AM. Since I'm trying to write to the database, I'm wondering if there isn't a security/permissions setting that isn't set properly on the server end of the world.
    I'll let you know how it works out.
    Mike
    mike@texaswebdevelopers.com
    http://www.texaswebdevelopers.com

  4. #4
    Join Date
    Feb 2002
    Location
    Campbellsport, Wisconsin
    Posts
    1,989
    That's why I threw the

    if adoRs.Supports(16778240) then ' Conn supports Updates


    in there...

    Should realy put an else in there that prints out the fact that the connection type don't support updates.

  5. #5
    Join Date
    Feb 2002
    Location
    Dallas, Texas
    Posts
    6
    I LOVE YA, Bud!!
    The "supports" code is a completely new one to me and it's another diamond that you've sent me.
    Turns out that the folder that I stuck the database in did NOT have correct permissions. Your code change (to filter the RS) is also "sweet". I'll post the content update editor example as soon as I'm finished with it.
    Thanks again!!
    Mike
    mike@texaswebdevelopers.com
    http://www.texaswebdevelopers.com

  6. #6
    Join Date
    Feb 2002
    Location
    Campbellsport, Wisconsin
    Posts
    1,989
    Yup I incorporated that into the functions I designed for data access...

    For readability the (Supports Method) you can call these Constants by name if you include "Adovbs.inc " in your source

    Constant
    Value
    Functionality



    adAddNew
    16778240
    You can use the AddNew method to add new records.

    adApproxPosition
    16384
    You can read and set the AbsolutePosition and AbsolutePage properties.

    adBookmark
    8192
    You can use the Bookmark property to access specific records.

    adDelete
    16779264
    You can use the Delete method to delete records.

    adHoldRecords
    256
    You can retrieve more records or change the next retrieve position without committing all pending changes and releasing all currently held records.

    adMovePrevious
    512
    You can use the MovePrevious or Move methods to move the current record position backward without requiring bookmarks.

    adResync
    131072
    You can update the cursor with the data visible in the underlying database.

    adUpdate
    16809984
    You can use the Update method to modify existing data.

    adUpdateBatch
    65536
    You can use batch updating to transmit changes to the provider in groups.

  7. #7
    Join Date
    Feb 2002
    Location
    Washington state, USA
    Posts
    1,355
    Please tell me you don't have those numbers memorized, bud. :):

    That trick is new to me, too. I'll have to try it out sometime. Thx!

    ~no_mac_jack

    BTW- Good to "see" you again, Mike! :woohoo:
    "He that would make his own liberty secure must guard even his enemy from oppression." - Thomas Paine

  8. #8
    Join Date
    Feb 2002
    Location
    Campbellsport, Wisconsin
    Posts
    1,989
    No... but when you forget where you put it amungst 90 gig's of other stuff... It can take longer to find than it does to just look it up...

    MS talks about the "Adovbs.inc" in the Filesystem Object sample...

  9. #9
    awasson's Avatar
    awasson is offline Administrator...... of what, were not quite sure.
    Join Date
    Feb 2002
    Location
    Vancouver BC Canada
    Posts
    1,018
    Nice Bud,

    Your "support", and "filter" aren't the run of the mill ASP/DB processes that see to be on every ASP Help site or book.

    Really cool.

    I'm just browsing the forum for the first time in the last week..... been really busy, doing a lot of what is being discussed here strangely enough.


    That's some nice stuff.

    I've been working on a couple of Content Management Systems as well. I used to seperate the processes into several pages but recently combined them into one super editing page allowing:
    1) Creation
    2) Editing
    3) Deleting
    4) Uploading images (for a staff directory)

    Deleting was the toughest process. It's easy to create a link that deletes the item, but you want to warn the editor that they are about to delete the item.

    JavaScript to the rescue. Along with a couple of trips back to the server.

    I plan at some point soon to package the whole deal up and maybe open it up as a full content management system in a sort of Open Source initiative.

    Anyway it's always nice to look at someone elses code and see something new.

    Cheers,
    Andrew

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •