Back to Top
The nuts and bolts for
Creative web development

Automatically set a session variable with the new record ID

Make this slight modification to an already great extension to place the just inserted record ID in to a session variable called NewID.  Then, you can reference the NewID in a new recordset. (There may be a better way to do this but it worked great for me using UD4 and SQL Server 7.0!)

In order to set a Session Variable to the just inserted record ID or "Identity" you can either modify the code or edit the extension itself. Here is what it should look like:

If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = "SET NOCOUNT ON;" & MM_editQuery & ";SELECT @" & "@IDENTITY AS Ident"
    Set rsLastIdent = MM_editCmd.Execute
    if NOT rsLastIdent.EOF then
    strLastIdent = rsLastIdent.Fields.Item("Ident").Value
         ' place the new record ID in a session variable called "NewID"
    Session("NewID") = strLastIdent
    end if
    MM_editCmd.ActiveConnection.Close 

Once you have modified the code like the red section above then Click on the + on the Data Bindings panel and select Session. Name it "NewID".  It will contain the just created record ID for you to use in new recordsets or whatever!  If you want to test it make the changes I mentioned, then create a blank page and drag the new Session variable you created called NewID on to the page.  Refer to that page when the form you created submits and the record ID of the just submitted form will show on that page.

Enjoy! 

Chris

Chris von Nieda

Chris von NiedaHello, my name is Chris von Nieda. Thanks for dropping by! I am a web developer, SEO and SEM. About 15 years ago I saw the Internet for the first time and was immediately hooked. Since then it has been my life, passion and hobby.

See All Postings From Chris von Nieda >>

Share this Article

Comments

RE: ASP.NET version

logan did you manage to crack this with ASP.NET? I am trying to do the same thing

ASP.NET Version

Anyone know how to do this in ASP.NET using Dreamweaver MX?  I have been trying to use the @@identity but no luck.  I also need this sesion variable to work with regular ASP pages.

ASP.NET Version

Anyone know how to do this in ASP.NET using Dreamweaver MX?  I have been trying to use the @@identity but no luck.  I also need this sesion variable to work with regular ASP pages.

Pass Identity without Session Variable

I have seen numerous articles out there that claim session variables are “evil” because of they take up web server resources.  So, after playing around with the code from DMX Zone, I made a couple alterations that eliminate the session variable and allows me to simply perform a “Request(“New_ID”)” on the page I have chosen as the redirect after submitting a record.

   If (Not MM_abortEdit) Then

    ' execute the insert

            Set MM_editCmd = Server.CreateObject("ADODB.Command")

    MM_editCmd.ActiveConnection = MM_editConnection

    MM_editCmd.CommandText = "SET NOCOUNT ON;" & MM_editQuery & ";SELECT @" & "@IDENTITY AS Ident"

    Set rsLastIdent = MM_editCmd.Execute

    if NOT rsLastIdent.EOF then

    strLastIdent = rsLastIdent.Fields.Item("Ident").Value

    end if

    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then

            MM_editRedirectUrl = MM_editRedirectUrl & "?New_ID=" & strLastIdent

      Response.Redirect(MM_editRedirectUrl)

    End If

(strLastIdent is added to the MM_editRedirectURL variable)

See all 17 Comments