|
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:
- Enter your insert command and setup your datasource for inserting.
- Append to the end of the insert (this can be done in the Source view of the page) the following:
-
; select @PK=SCOPE_IDENTITY();
- 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" />
- 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.
|