Solved

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

Posted on 2012-04-03
6
383 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now