Manoj Singh Manoj Singh - 7 months ago 17
Vb.net Question

How to get last inserted identity in Formview Inserted event using VB.net

I am using VB.net (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 VB.net

Thanks.

Best Regards,
MS

Answer

I solved my above problem using below logic

I changed my SQL insert Procedure and added new Parameter

@OrgID int OUTPUT

and after insert command

I used

SET @OrgID = SCOPE_IDENTITY()

RETURN

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.

Cheers!

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