Solved

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

Posted on 2012-04-03
6
389 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

20 Experts available now in Live!

Get 1:1 Help Now