How to get last inserted id in Formview Injected event using VB.net
I am using VB.net (FormView and ObjectDataSource) and Sql Server 2005.
I want the latter to insert @@ identity into FormView1_ItemInserted table
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
End Sub
My problem is that I want to redirect my FormView to readonly mode after FormView1_ItemInserted , but for that I need to show the inserted record in readonly mode and this is only possible if I get the last insert @@ identity . Could you please let me know what changes I need to make in my application, procedures and code to achieve this.
Please suggest some sample code! using VB.net
Thank.
Best wishes MC
I solved my problem above using the following logic
I changed my SQL insert routine and added a new parameter
@OrgID int OUTPUT
and after the insert command
I used
SET @OrgID = SCOPE_IDENTITY()
RETURN
Further in my application after reconfiguring my ObjectDataSource object I got below parameter
<asp:Parameter ConvertEmptyStringToNull="true" Name="OrgID" Type="Int32"
Direction="Output" />
in my insert objectdatasource Parameter
I am writing below code in my inserted ObjectDataSource.
Protected Sub RAOOrganisationDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles RAOOrganisationDataSource.Inserted
Dim OrgID As Integer = e.OutputParameters("OrgID")
Session("OrgID") = OrgID
End Sub
And this way I got my last set OrgID in the session.
Hooray!
Please let me know if there are any mistakes when using the concept above
Actually the easiest way to do this is to add an onInserted event to your data source, which in my case was LinqDatasource.
protected void lnqInsert_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
Session["RecentInsertedClass"] = ((Class)e.Result).ID;
}