Link to home
Start Free TrialLog in
Avatar of mastertrac
mastertrac

asked on

How to retrieve Scope_Identity parameter in ItemInserted Event

I wish to use a FormView to insert a record but then stay on the same page but change to edit mode.  I understand I have a stored proc to do the insert that that has a select Scope_Identity() statement at the end.  I also understand that I need to use the ItemInserted event to retrieve the Identity value.  

I need the c# code to retrieve the parameters value from my sql data source.

Thanks
Avatar of sabeesh
sabeesh
Flag of United States of America image

C#
object obj = command.ExecuteScalar();
                    if (obj != null)
                    {
                        int id = int.Parse(obj.ToString());
                    }

Sql code
//

CREATE PROCEDURE [dbo].[CA_SP_InsertResource]
(
      
)
AS

BEGIN
      INSERT INTO table....
      select @@identity from table
END
GO
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mastertrac
mastertrac

ASKER

I understand the sqlcode.  However, I was led to believe that I could let the .net FormView and SqlDataSource d the inserting of the record using the stored procedure and then in the ITEMINSERTED event retrieve the identity through a parameter or something.  So the C# code above doesn't make sense in this scenario. I know how to use executeSaler but I want to let the the formview insert the record for me, retrieve the identity, then using the identity change to edit mode.   Is this possible?  I just tried doing an executescaler in the ItemInserted event of the formview and it did not retrieve the identity.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial