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?
ASKER
That does not return the id of the new record!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did exactly as you sugessted but I got the following error on this line: e.Command.Parameters["@Dep artmentID" ].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!
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
either you need to generate and insert it - or set the feild to be an autonumber
ASKER
>is DepartmentID an auto generated number?
Yup
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
if it has - maybe try
SET @DepartmentID = SCOPE_IDENTITY()
instead of
SELECT @DepartmentID = @@IDENTITY
ASKER
Same problem!
and the row is being inserted?
can u post your code?
can u post your code?
ASKER
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 .DetailsVi ewInserted EventArgs' .
If True Then
If e.Exception Is Nothing Then
Label1.Text = [String].Format("Departmen t '{0}' successfully added.", e.Command.Parameters("@Dep artmentID" ).Value)
Else
Label1.Text = "Unable to add department."
e.ExceptionHandled = True
End If
End If
'Command' is not a member of 'System.Web.UI.WebControls
If True Then
If e.Exception Is Nothing Then
Label1.Text = [String].Format("Departmen
Else
Label1.Text = "Unable to add department."
e.ExceptionHandled = True
End If
End If
refer to -> FormViewInsertedEventArgs.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formviewinsertedeventargs.values.aspx