We help IT Professionals succeed at work.

does sqldatasource insert method return anything like primary key?

Medium Priority
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?
Watch Question

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.


the bit to note is the part:

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

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


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

Ask the Experts

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:

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.


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.