How to get new added database record identity in ASP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bon

    How to get new added database record identity in ASP

    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_proce dure_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.ex ecute("Exec sp_stored_proce dure_name
    @param1='" & Request("Name") & "',@param2= '" & Request("Addres s") &
    "'")
    session("Return edScopeID") = objrs_dr.Fields ("SPReturnedID" )
    %>
    alert ( <%=session("Ret urnedScopeID")% );
    ....
    }

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

    Cheers
    Bon

Working...