Returns the primary key of the inserted row.

Last post 05-15-2008 9:02 AM by smcirish. 6 replies.

Sort Posts:

  • Returns the primary key of the inserted row.

    01-09-2006, 12:15 PM
    • Loading...
    • Patheon
    • Joined on 10-25-2005, 8:15 PM
    • Posts 13
    Does anyone have a working example in Visual Web Developer 2005 of  using a stored procedure to insert a record and return the primary key of the inserted row?
  • Re: Returns the primary key of the inserted row.

    01-09-2006, 12:45 PM
    • Loading...
    • bmains
    • Joined on 10-22-2004, 8:20 AM
    • Posts 4,861
    • TrustedFriends-MVPs

    Hey,

    Your stored procedure has to do the insert, then select the ID value, then in your code, when you execute through a Command, use the ExecuteScalar() method of execution, which returns the first value of a single field select statement.

    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: Returns the primary key of the inserted row.

    01-09-2006, 1:28 PM
    • Loading...
    • Patheon
    • Joined on 10-25-2005, 8:15 PM
    • Posts 13
    Hi Brian, Thanks for the reply. I have created a Stored procedure to insert the record... and it inserts fine. Could you give me an example of what you mean? Thanks in advance.
  • Re: Returns the primary key of the inserted row.

    01-10-2006, 7:58 AM
    • Loading...
    • bmains
    • Joined on 10-22-2004, 8:20 AM
    • Posts 4,861
    • TrustedFriends-MVPs

    Stored procedure has to:

    insert into table (..) values (..)
    select @@identity

    Then in your code, setup the command, then do:

    Dim objValue As Object = objCommand.ExecuteScalar()
    if (IsNumeric(objValue)) then
       Dim intID as Integer = ctype(objValue, Integer)
    end if

    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: Returns the primary key of the inserted row.

    01-10-2006, 11:06 AM
    • Loading...
    • Patheon
    • Joined on 10-25-2005, 8:15 PM
    • Posts 13

    Big Smile [:D] Thanks Brian.... I've now got it to work.... I can retrieve the Instance ID of the newly created row. Now I have a new problem... The record is being inserted Twice.  I'm using a SqlDataSource with the commandfield Insert Button, I've placed the code you gave me in the

    Protected Sub SqlDataSource1_Inserted

    the code you gave me, the ExecuteScalar() does it cause the stored procedure to be fired again? If so, then where and how can I avoid this situation?

     

  • Re: Returns the primary key of the inserted row.

    01-10-2006, 12:34 PM
    • Loading...
    • bmains
    • Joined on 10-22-2004, 8:20 AM
    • Posts 4,861
    • TrustedFriends-MVPs
    Oh OK, sorry, I didn't know you were using a SqlDataSource; I'm not sure of the best way to do this with a Data Source control, but maybe by adding another parameter as an output parameter to the stored procedure, then add this to your insertparameters collection, and check that parameters defaultvalue property (sqlds.InsertParameters["RETURN_VALUE"].DefaultValue).
    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: Returns the primary key of the inserted row.

    05-15-2008, 9:02 AM
    • Loading...
    • smcirish
    • Joined on 04-16-2007, 5:27 PM
    • Texas
    • Posts 210

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=54

    Here is an example of Scope_Identity  with Stored Procedures.

    -smc

    ~ Remember To Mark The Posts Which Helped You As The ANSWER ~
    Filed under:
Page 1 of 1 (7 items)