Here's a sweet way to get the last inserted ID when using an SqlDataSource to do your insert.  First add ;SET @NewId = Scope_Identity() to the end of your InsertCommand, and to the InsertParameters add:

<
asp:Parameter Name="NewId" Type="Int32" Direction="Output"
/>

Then you can get the ID in the data source's Inserted event like this:

protected
void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
Int32 id = (Int32)e.Command.Parameters["@NewId"].Value;
}

Comments are closed.