Manoj Singh Manoj Singh - 1 year ago 78 Question

How to get last inserted identity in Formview Inserted event using

I am using (FormView and ObjectDataSource) and Sql Server 2005.

I want to get last inserted @@identity in table on FormView1_ItemInserted

Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted

End Sub

My issue 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 that is only possible if I get my last inserted @@identity. Can you please let me know what the changes I need to do in my application, Procedures and code to achive this.

Please suggest with example code! using


Best Regards,

Answer Source

I solved my above problem using below logic

I changed my SQL insert Procedure and added new Parameter


and after insert command

I used



Further in my Application after reconfiguring my ObjectDataSource, I got below parameter

    <asp:Parameter ConvertEmptyStringToNull="true" Name="OrgID" Type="Int32" 
        Direction="Output" />

in my objectdatasource insert Parameter

I write below code in my ObjectDataSource inserted event.

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 thus I got my latest inserted OrgID in Session.


Please do let me know if there is any errors while using above concept