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

Get Data Key Value of newly inserted record

3 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick Borage
Top achievements
Rank 1
Rick Borage asked on 29 Oct 2010, 09:32 PM
I need to find the data key value (the identity value) of a record immediately after it is inserted.  I tried the GetDataKeyValue method on the GridEditFormInsertItem in the ItemInserted event but it is coming back null.  See below...

 

GridEditFormInsertItem i = (GridEditFormInsertItem)e.Item;

 

 

string keyValue = i.GetDataKeyValue("id").ToString();

I am able to this in the ItemUpdated using almost the exact same code...

 

 

GridEditableItem i= (GridEditableItem)e.Item;

 

 

string keyValue = i.GetDataKeyValue("id").ToString();

and that works.

Anyone know how to do this?

 

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2010, 07:33 AM
Hello Rick,

In order to find the DataKeyValue of newly inserted row, first you need to find the last inserted record. Then in PreRender find the DataKeyValue of newly inserted record. You can use the logic in the following Code Library to achieve this.
Select Last Inserted Row

Thanks,
Princy.
0
Rick Borage
Top achievements
Rank 1
answered on 01 Nov 2010, 03:14 PM
That's essentially what I did, only I used the ItemInserted event.


protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
      if (e.Exception != null)
      {
        e.ExceptionHandled = true;
        SetMessage("Record cannot be inserted. Reason: " + e.Exception.Message);
      }
      else
      {
        SetMessage("New record is inserted!");
  
        // Get ID of Item inserted
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
        String SqlStatement;
        SqlStatement = "SELECT MAX(TransactionId) FROM Transactions WHERE DealerId = '" + RadComboBoxDealer.SelectedValue.ToString();
        SqlCommand cmd = new SqlCommand(SqlStatement, cnn);
        cmd.Connection.Open();
        String id = cmd.ExecuteScalar().ToString();
        cmd.Connection.Close();
  
        // Did my handling code here....
        }
      }
    }

This works, it just seems like there could be a better way to do this.  If multiple people were working in the database at the same time then there is a chance this would return the wrong record.  I actually included the where clause in my statement to help ensure I am returning the MAX ID from the correct subset of records.
0
Marin
Telerik team
answered on 02 Nov 2010, 02:47 PM
Hello Rick Borage,

In the ItemInserted event you can retrieve all the values from the Insert form that you have entered manually by using for example ExtractValues method of GridEditFormInsertItem
GridEditFormInsertItem insertItem = e.Item as GridEditFormInsertItem;
insertItem.ExtractValues(dictionary);
or the GridTableView.ExtractValuesFromItem method as shown in this demo. Normally it depends on the settings on the DataKey field in the database (if it is configured as autoincrement it automatically sets in the new value after the item is inserted; if you have allowed the user to insert the ID (i.e. the datakey field) from the insert form then you can easily get this value as shown in the above way)
The GetDataKeyValue methos works only for existing items in the grid structure, but since the new item has just been inserted it (and its DataKey value) exists only in the database.

All the best,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Rick Borage
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rick Borage
Top achievements
Rank 1
Marin
Telerik team
Share this question
or