Solved

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

Posted on 2012-04-03
6
409 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
  • 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp classic find word in string and get its value 7 32
How can I put values from different columns in a single cell 11 34
Replace value 2 27
Asp response.write to clients 6 32
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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