SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

Last post 07-09-2008 10:23 AM by jfierst. 4 replies.

Sort Posts:

  • SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

    07-09-2008, 9:18 AM
    • Member
      87 point Member
    • jfierst
    • Member since 09-10-2007, 2:51 PM
    • Greenville, Pa
    • Posts 209

    I have a formview where the new record is created, but only sometimes does it pull the id of the recently inserted record.  Can you guys look and tell me whats wrong?

     

    QUERY>>>>>>>>>>>>>>>>>> 

     <asp:SqlDataSource ID="FormviewSqlDataSource1" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:SiteDataConnString %>"
                                       InsertCommand="INSERT INTO [COUPLE_REGISTRATION] ([DATE], [CLEARANCE], [BW_NAME1], [BW_NAME2], [BW_ADDY1], [BW_ADDY2], [BW_CITY], [BW_STATE], [BW_ZIP], [BW_PHONE], [BW_EMAIL], [BW_RELAT], [BW_RELIG], [BW_RELAT2], [BW_RELIG2], [BW_RELAT3], [BW_RELIG3], [GH_NAME1], [GH_NAME2], [GH_ADDY1], [GH_ADDY2], [GH_CITY], [GH_STATE], [GH_ZIP], [GH_PHONE], [GH_EMAIL], [GH_RELAT], [GH_RELIG], [GH_RELAT2], [GH_RELIG2], [GH_RELAT3], [GH_RELIG3]) VALUES (@DATE, @CLEARANCE, @BW_NAME1, @BW_NAME2, @BW_ADDY1, @BW_ADDY2, @BW_CITY, @BW_STATE, @BW_ZIP, @BW_PHONE, @BW_EMAIL, @BW_RELAT, @BW_RELIG, @BW_RELAT2, @BW_RELIG2, @BW_RELAT3, @BW_RELIG3, @GH_NAME1, @GH_NAME2, @GH_ADDY1, @GH_ADDY2, @GH_CITY, @GH_STATE, @GH_ZIP, @GH_PHONE, @GH_EMAIL, @GH_RELAT, @GH_RELIG, @GH_RELAT2, @GH_RELIG2, @GH_RELAT3, @GH_RELIG3); SELECT @RETURN_VALUE = SCOPE_IDENTITY()">

     

    PARAMS>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    <InsertParameters>
                                         <asp:Parameter Name="RETURN_VALUE" Direction="Output" Type="Int32" />
                                        </InsertParameters>

     

    CODE BEHIND>>>>>>>>>>>>>>>>>>>>>>>>

     

        Protected Sub FormviewSqlDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles FormviewSqlDataSource1.Inserted

            Session("couple_id") = e.Command.Parameters("RETURN_VALUE").Value
    End Sub

  • Re: SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

    07-09-2008, 9:27 AM
    • Contributor
      4,518 point Contributor
    • tompy_nation
    • Member since 02-28-2007, 10:45 AM
    • Gent
    • Posts 1,153

    use @@IDENTITY istead of SCOPE_IDENTITY()

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

    07-09-2008, 9:34 AM
    • Member
      87 point Member
    • jfierst
    • Member since 09-10-2007, 2:51 PM
    • Greenville, Pa
    • Posts 209

    I get the same error with that also.

    sys.webforms.pagerequestmanagerservererrorexception: object refrence not set to an instance of an object

  • Re: SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

    07-09-2008, 10:14 AM
    Answer
    • Member
      710 point Member
    • bihagsasp.net
    • Member since 01-25-2008, 2:40 PM
    • Ahmedabad
    • Posts 135

    Hi jfierst,

    You are missing @ for parameter name.

    Following statement 

    Session("couple_id") = e.Command.Parameters("RETURN_VALUE").Value

    should be  

    Session("couple_id") = e.Command.Parameters("@RETURN_VALUE").Value

     


    Please, 'Mark as Answer' if this post has helped you!

    - Bihag Thaker

    http://bihagthaker.blogspot.com/

  • Re: SELECT @RETURN_VALUE = SCOPE_IDENTITY() - Only Works Sometimes?

    07-09-2008, 10:23 AM
    • Member
      87 point Member
    • jfierst
    • Member since 09-10-2007, 2:51 PM
    • Greenville, Pa
    • Posts 209

    You're right, I was, but the error is actually being caused by another error, and not this one.  I had other code causing the problem.  Running in VS to debug is usually the answer.

     Moderator, please close / delete this entire thread.

Page 1 of 1 (5 items)