We help IT Professionals succeed at work.

does sqldatasource insert method return anything like primary key?

joshuadavidlee
on
Medium Priority
1,596 Views
Last Modified: 2007-12-19
i have a primary key column that is uniqueidentifier and has default to newID() or I have another table with autoincrment set to 1 for a primary key column of type int.  I was just wondering when using vs2005 and calling the insert method using the sqldatasource object if i can find out what key value the database assigned to the new row that was inserted?  i have tried cating the sqldatasource inserted event and not sure if the answer is in there or not?
Comment
Watch Question

Commented:
Have you tried using output parameters? These can be used to get the identity after the insert. I could find one example on the web of this.

http://dotnetjunkies.com/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/RetValAndOutputParamsInsert.src

the bit to note is the part:

        <InsertParameters>
          <asp:Parameter Name="contactName" Type="String" />
          <asp:Parameter Direction="Output" Name="contactID" Type="Int32" Size="4" />
        </InsertParameters>

This uses a stored procedure to do the insert, which will assign @@IDENTITY to the parameter @contactID after the insert.

Andy

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
By the way, you want to be careful about using @@IDENTITY. It can, under certain circumstances, return a different identity value as it has server-level scope. Have a look at this to start off with understanding @@IDENTITY, SCOPE_IDENTITY() and IDENT_CURRENT() as well as INSERT ... OUTPUT:

http://www.dbazine.com/sql/sql-articles/cook18
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.