How to get new added database record identity in ASP

B

Bon

I created a stored procedure for inserting new data to database and
then select scope_identity. The returned ID result is correct when i
run the SP directly. But, ASP recordset return a wrong result. It
always return the previous ID. For example, the current ID is 100.
After I call the SP from ASP page, SP is run and the ID is 101. But,
the ASP recordset would return 100. Any suggestion?

My SP code:

CREATE PROCEDURE sp_stored_procedure_na,e
@param1 char (10),
@param2 varchar(255)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO myTableName
(
Name,
Address
)
VALUES
(
@param1,
@param2
)
SELECT SPReturnedID = SCOPE_IDENTITY()
END

My ASP code is embedded in Javascript:

<%
Dim objrs-dr
%>

function AddRow_clk()
{
'use xmlhttp to pass parameters
....
<%
Set objrs_dr = dbconnection.execute("Exec sp_stored_procedure_name
@param1='" & Request("Name") & "',@param2='" & Request("Address") &
"'")
session("ReturnedScopeID") = objrs_dr.Fields("SPReturnedID")
%>
alert ( <%=session("ReturnedScopeID")%> );
....
}

I use Javascript to test the returned result. It always returns 100
instead of 101. Could you give me some suggestions?

Cheers
Bon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top