Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retreive last id from record inserted in db (ASP/VB)

Posted on 2012-04-03
6
Medium Priority
?
440 Views
Last Modified: 2012-04-04
Hi,

I am using dreamweaver, ms sql 2008 and classic ASP/VB page. Below is the code that inserts a record into the database. I do need however to add some code in there to retreive the last id inserted and save it in a session variable named: Receiptidtemp

I have that type of code for an ASP/Javascript page but not for ASP/VB. If someone could help with this would be great :)

Thanks.
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:pateljitu
ID: 37801508
Please submit your code.

Also you could use store procedure for insert statement and return last inserted row Id which you could assign to variable "Receiptidtemp"

Here is an example on using store-procedure and return value:
http://stackoverflow.com/questions/4979945/return-value-in-stored-procedure-sql-server

Method 3 in this article:
http://support.microsoft.com/kb/164485
0
 

Author Comment

by:amucinobluedot
ID: 37801634
Yeah, i dont wanna go there for this one. If I can get this done with ASP then will keep things simple. For example with ASP/JS all I add is this lines of code in between the insert statement, something similar in ASP/VB would be great !

-- ASP/JS code--

           <!--set up Auto Number retrieval for SQL Server -->
     var rsNewAutoIncrement = MM_editCmd.ActiveConnection.Execute("select @@identity")
     Session("CaseIDOpened") = rsNewAutoIncrement(0).Value
     <!--rsNewAutoIncrement.Close -->
     <!--var rsNewAutoIncrement = Nothing -->
     <!--end retrieval -->
0
 
LVL 15

Accepted Solution

by:
pateljitu earned 2000 total points
ID: 37801907
If you have the working code in JS for VB please try this line, it is setting the recordset.

set rsNewAutoIncrement = MM_editCmd.ActiveConnection.Execute("select @@identity")
Session("CaseIDOpened") = rsNewAutoIncrement(0).Value
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:amucinobluedot
ID: 37805759
I don't know in which part of my code to insert those lines. Here is the insert code in ASP/VB

<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
' IIf implementation
Function MM_IIf(condition, ifTrue, ifFalse)
  If condition = "" Then
    MM_IIf = ifFalse
  Else
    MM_IIf = ifTrue
  End If
End Function
%>
<%
If (CStr(Request("MM_insert")) = "form1") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_eimmigration_STRING
    MM_editCmd.CommandText = "INSERT INTO dbo.Activities_Misc (caseid, firmid, RcptNo, RcptDate, Filingctr, FormName, [Class], Filedon, NoticeDate, Approvedon, Validfromdate, Validtodate, NoticeType, FilingCurrentDate, PetName, BenName, rec_notes, PrioDate, Chargeability, CaseType, Immigrant_Preference, PriorityCurrentDate, NotificationEmail) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 5, 1, -1, MM_IIF(Request.Form("CaseId"), Request.Form("CaseId"), null)) ' adDouble
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 5, 1, -1, MM_IIF(Request.Form("FirmId"), Request.Form("FirmId"), null)) ' adDouble
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 250, Request.Form("RcptNo")) ' adLongVarChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 135, 1, -1, MM_IIF(Request.Form("RcptDate"), Request.Form("RcptDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 202, 1, 50, Request.Form("FilingCtr")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param6", 202, 1, 40, Request.Form("FormName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param7", 202, 1, 200, Request.Form("Class")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param8", 135, 1, -1, MM_IIF(Request.Form("FiledOn"), Request.Form("FiledOn"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param9", 135, 1, -1, MM_IIF(Request.Form("NoticeDate"), Request.Form("NoticeDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param10", 135, 1, -1, MM_IIF(Request.Form("ApprovedOn"), Request.Form("ApprovedOn"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param11", 135, 1, -1, MM_IIF(Request.Form("ValidFromDate"), Request.Form("ValidFromDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param12", 135, 1, -1, MM_IIF(Request.Form("ValidToDate"), Request.Form("ValidToDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param13", 202, 1, 100, Request.Form("NoticeType")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param14", 135, 1, -1, MM_IIF(Request.Form("FilingCurrentDate"), Request.Form("FilingCurrentDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param15", 202, 1, 150, Request.Form("PetName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param16", 202, 1, 150, Request.Form("BenName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param17", 202, 1, 255, Request.Form("Rec_Notes")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param18", 135, 1, -1, MM_IIF(Request.Form("PrioDate"), Request.Form("PrioDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param19", 202, 1, 50, Request.Form("Chargeability")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param20", 201, 1, 50, Request.Form("CaseType")) ' adLongVarChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param21", 202, 1, 50, Request.Form("Immigrant_Preference")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param22", 135, 1, -1, MM_IIF(Request.Form("PriorityCurrentDate"), Request.Form("PriorityCurrentDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param23", 202, 1, 300, Request.Form("notificationemail")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    ' append the query string to the redirect URL
    Dim MM_editRedirectUrl
    MM_editRedirectUrl = "Updatecasereceipts.asp"
    If (Request.QueryString <> "") Then
      If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
        MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      Else
        MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
      End If
    End If
    Response.Redirect(MM_editRedirectUrl)
  End If
End If
%>
0
 

Author Comment

by:amucinobluedot
ID: 37805789
Apologize, this is the code:

---

<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
' IIf implementation
Function MM_IIf(condition, ifTrue, ifFalse)
  If condition = "" Then
    MM_IIf = ifFalse
  Else
    MM_IIf = ifTrue
  End If
End Function
%>
<%
If (CStr(Request("MM_insert")) = "form1") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_eimmigration_STRING
    MM_editCmd.CommandText = "INSERT INTO dbo.Activities_Misc (caseid, firmid, RcptNo, RcptDate, Filingctr, FormName, [Class], Filedon, NoticeDate, Approvedon, Validfromdate, Validtodate, NoticeType, FilingCurrentDate, PetName, BenName, rec_notes, PrioDate, Chargeability, CaseType, Immigrant_Preference, PriorityCurrentDate, NotificationEmail) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 5, 1, -1, MM_IIF(Request.Form("CaseId"), Request.Form("CaseId"), null)) ' adDouble
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 5, 1, -1, MM_IIF(Request.Form("FirmId"), Request.Form("FirmId"), null)) ' adDouble
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 250, Request.Form("RcptNo")) ' adLongVarChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 135, 1, -1, MM_IIF(Request.Form("RcptDate"), Request.Form("RcptDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 202, 1, 50, Request.Form("FilingCtr")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param6", 202, 1, 40, Request.Form("FormName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param7", 202, 1, 200, Request.Form("Class")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param8", 135, 1, -1, MM_IIF(Request.Form("FiledOn"), Request.Form("FiledOn"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param9", 135, 1, -1, MM_IIF(Request.Form("NoticeDate"), Request.Form("NoticeDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param10", 135, 1, -1, MM_IIF(Request.Form("ApprovedOn"), Request.Form("ApprovedOn"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param11", 135, 1, -1, MM_IIF(Request.Form("ValidFromDate"), Request.Form("ValidFromDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param12", 135, 1, -1, MM_IIF(Request.Form("ValidToDate"), Request.Form("ValidToDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param13", 202, 1, 100, Request.Form("NoticeType")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param14", 135, 1, -1, MM_IIF(Request.Form("FilingCurrentDate"), Request.Form("FilingCurrentDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param15", 202, 1, 150, Request.Form("PetName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param16", 202, 1, 150, Request.Form("BenName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param17", 202, 1, 255, Request.Form("Rec_Notes")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param18", 135, 1, -1, MM_IIF(Request.Form("PrioDate"), Request.Form("PrioDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param19", 202, 1, 50, Request.Form("Chargeability")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param20", 201, 1, 50, Request.Form("CaseType")) ' adLongVarChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param21", 202, 1, 50, Request.Form("Immigrant_Preference")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param22", 135, 1, -1, MM_IIF(Request.Form("PriorityCurrentDate"), Request.Form("PriorityCurrentDate"), null)) ' adDBTimeStamp
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param23", 202, 1, 300, Request.Form("notificationemail")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    ' append the query string to the redirect URL
    Dim MM_editRedirectUrl
    MM_editRedirectUrl = "SPSetdefaultreceiptfirst.asp"
    If (Request.QueryString <> "") Then
      If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
        MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      Else
        MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
      End If
    End If
    Response.Redirect(MM_editRedirectUrl)
  End If
End If
%>

---
0
 

Author Closing Comment

by:amucinobluedot
ID: 37805800
Worked great thanks !
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question