Blog

ATAYE HOME Blog Home Account Search
How to return identity after insert using DotNET Sql Data source without using Stored Procedure

Here is a little trick i use to return the identity from a PK column after doing an insert with the Sql Data source component.  Since this is done at the data source level it can be used with any databound control like the FormView, DetailsView or any other 2-way databound control.

To make this happen just do the following:

  1. Enter your insert command and setup your datasource for inserting.
  2. Append to the end of the insert (this can be done in the Source view of the page) the following:
    • ; select @PK=SCOPE_IDENTITY();

  3. Now add a new parameter to the <InsertParameters> node of the SqlDataSource (Change the datatype to match):
    • <asp:Parameter Name="PK" Direction="Output" Type="Int16" />

  4. No you can access the new PK value in the 'Inserted' event after the data has been inserted eg:
    • this.Response.Write(e.Command.Parameters["@PK"].Value.ToString());

So what happenes is after the insert the sql sets the new identity to the parameter @PK which is declared on the data source as an output parameter.  Then the value can be read after the insert through the Inserted event.

I hope this helps.

Happy coding.

 

<p>MBT Masai Barefoot Technology. MBT unique design, multi-level, the only cure is designed to simulate walking in sand 'similar to walking in the natural environment in Kenya Masai'. This creates a natural, MBT shoes. uneven walking surfaces, and use of physical force and stability of the major muscle groups, mbt shoes training the body to move correctly. Discount MBT shoes. It has a positive effect on the whole body,help improve posture, reuce back pain, and alleviate joint pressure. New MBT sale will be your best healthy life partner. </p>
Comment By buy At 22/07/2010 5:00 PM
Well-known guitarist Pete Wentz hosted the grand New Year countdown party at the Los Angeles hotel The Mirage's top nightclub JET, and ceremoniously showed a new designed pair of Supra Shoes – Gradient to the world. As we all know, Wentz is a superstar fans of Supra Footwear, we have already witnessed for times that he wearing Supra Society and other kinds of Supra shoes. For more Mens Supra Shoes, welcome to here.
Comment By air jordan At 27/07/2010 4:35 PM
Please logon or register to comment.

 
copyright 2007 Ataye.com.au