Link to home
Start Free TrialLog in
Avatar of Abdu_Allah
Abdu_Allah

asked on

In my ItemInserted event for the formview control, how do I get the key value of the newly inserted row?

Hello, I have a formview control with DefaultMode="Insert".  In my ItemInserted event, how do I get the key value of the newly inserted row?
Avatar of Si_Hibbard
Si_Hibbard
Flag of United States of America image

you will need to access the values property of the FormViewInsertedEventArgs

refer to -> FormViewInsertedEventArgs.Values Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formviewinsertedeventargs.values.aspx

Avatar of Abdu_Allah
Abdu_Allah

ASKER

That does not return the id of the new record!
ASKER CERTIFIED SOLUTION
Avatar of Si_Hibbard
Si_Hibbard
Flag of United States of America 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
I did exactly as you sugessted but I got the following error on this line:  e.Command.Parameters["@DepartmentID"].Value
the error is: Conversion from type 'DBNull' to type 'Integer' is not valid.

It seems that the the value of that parameters is null!
is DepartmentID an auto generated number?
either you need to generate and insert it - or set the feild to be an autonumber
>is DepartmentID an auto generated number?

Yup
is the insert working correctly, ie when you look in the table the row has been added?
is the insert working correctly, ie when you look in the table the row has been added?

if it has - maybe try

SET @DepartmentID = SCOPE_IDENTITY()

instead of

SELECT @DepartmentID = @@IDENTITY
Same problem!
and the row is being inserted?

can u post your code?
Thanks, it works fine now.
I tried to convert the above to vb.net but get an error -
'Command' is not a member of 'System.Web.UI.WebControls.DetailsViewInsertedEventArgs'.      
        If True Then
            If e.Exception Is Nothing Then
                Label1.Text = [String].Format("Department '{0}' successfully added.", e.Command.Parameters("@DepartmentID").Value)
            Else
                Label1.Text = "Unable to add department."
                e.ExceptionHandled = True
            End If
        End If