This is a migrated thread and some comments may be shown as answers.

Parent, Child, retrieve DB Generated Key

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Krauska
Top achievements
Rank 1
Eric Krauska asked on 11 Feb 2009, 03:51 PM
I am having an issue when inserting the first child in a parent/child grid.  The insert is sent to the DB and the stored procedure inserts the correct data and generates a guid for the key on the newly inserted row.  I know radGrid is getting this guid back, but I can't figure out how to get it in the ItemInserted event.  Code in the iteminserted:
            GridEditableItem scope = e.Item as GridEditableItem; 
            try 
            { 
               CalculateDates((Guid)scope.OwnerTableView.DataKeyValues[scope.ItemIndex]["ScopeID"]); 
            } 

The issue is that scope.ItemIndex is always -1 and DataKeyValues.Count == 0.  How else can I extract the new key?


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Feb 2009, 06:35 AM
Hi,

You can use the Inserted event of the SqlDataSource to determine the ID of the newly created record . First  you need to modify the insert query:

InsertCommand="INSERT INTO Table(...) VALUES (...); SELECT @ScopeID= @@IDENTITY"

You can change "@@IDENTITY" to "SCOPE_IDENTITY()", if you are using SQL Server.

Then, add the following parameter to the InsertParameters:

<asp:Parameter Direction=Output Name="ScopeID" Type="Int64" />

In the Inserted event of the SqlDataSource you can obtain the output parameter

protected void SqlDataSource1 _Inserted(object sender, SqlDataSourceStatusEventArgs e)  
{  
    Int64 scopeID= (Int64) e.Command.Parameters["@ScopeID"].Value;  
}

Thanks,
Princy
Tags
Grid
Asked by
Eric Krauska
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or