Retrieving last inserted ID using Stored Procedures

I am running a Stored Procedure to insert a record, then I use the last ID of the inserted record to run another SP in a loop.
The code to retrieve the last ID worked in another page but for some reason not clear to me its not working on this one.

Here is the SP and the code at the end to retrieve the last ID and make the variable = to such ID.


Dim AddFormstoCase__CaseId
AddFormstoCase__CaseId = "0"
if(Request("caseid")        <> "") then AddFormstoCase__CaseId = Request("caseid")       

Dim AddFormstoCase__FirmId
AddFormstoCase__FirmId = "0"
if(Session("FirmId")        <> "") then AddFormstoCase__FirmId = Session("FirmId")       

Dim AddFormstoCase__fname
AddFormstoCase__fname = "0"
if(FormsFiltered("FormName")        <> "") then AddFormstoCase__fname = FormsFiltered("FormName")       

Dim AddFormstoCase__fdescription
AddFormstoCase__fdescription = "0"
if(FormsFiltered("FormDesc")        <> "") then AddFormstoCase__fdescription = FormsFiltered("FormDesc")       

Dim AddFormstoCase__Qid
AddFormstoCase__Qid = "0"
if(FormsFiltered("Qid")        <> "") then AddFormstoCase__Qid = FormsFiltered("Qid")       

Dim AddFormstoCase__mainuserid
AddFormstoCase__mainuserid = "0"
if(rs_case("userid")       <> "") then AddFormstoCase__mainuserid = rs_case("userid")      


set AddFormstoCase = Server.CreateObject("ADODB.Command")
AddFormstoCase.ActiveConnection = MM_bluedot_STRING
AddFormstoCase.CommandText = "dbo.AddFormstoCase"
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@RETURN_VALUE", 3, 4)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@CaseId", 3, 1,4,AddFormstoCase__CaseId)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@FirmId", 3, 1,4,AddFormstoCase__FirmId)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@fname", 200, 1,40,AddFormstoCase__fname)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@fdescription", 200, 1,255,AddFormstoCase__fdescription)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@Qid", 3, 1,4,AddFormstoCase__Qid)
AddFormstoCase.Parameters.Append AddFormstoCase.CreateParameter("@mainuserid", 3, 1,4,AddFormstoCase__mainuserid)
AddFormstoCase.CommandType = 4
AddFormstoCase.CommandTimeout = 0
AddFormstoCase.Prepared = true


<!--#End of of SP-->

Set rs = AddFormstoCase.Execute()

Open in new window

This is the error I get:  

ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/bluedot/Intranet/Cases/forms_SPadd.asp, line 467

Line 467 is:   LASTINSERTEDID = rs(0)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
can you detect any error if run the SP above in your ASP page? it could be some error out there so that your SP is not returning a resultset and hence it causes the error in your ASP page?

alternatively, you may post the scripts of SP: AddFormstoCase here for us to diagnose
AleksAuthor Commented:
No error, in fact it inserts the forms just fine.  :$
Big MontySenior Web Developer / CEO of Commented:
The error indicates no values are being returned.  Make sure your SP returns a value, teeth running it with all of the values you're using in your code
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

AleksAuthor Commented:
ok .. Ill double check.
You call the Execute method twice. It is incorrect. Call it once only:


<!--#End of of SP-->

Set rs = AddFormstoCase.Execute()

Open in new window

The correct code will be:
' AddFormstoCase.Execute() - comment out this line


<!--#End of of SP-->

Set rs = AddFormstoCase.Execute()

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
I was missing some in the Stored Procedure ... sigh ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.