hotcoder
hotcoder

Reputation: 3256

Get primary key column value after inserting record using DetailsView and EntityDataSource

I'm using DetailsView with EntityDataSource and binding EntityDataSource directly with Entity Model. I want to get the primary key value after record has been inserted. How can I get it either in

protected void detailsVewUser_ItemInserted(object sender, DetailsViewInsertedEventArgs e)

or

protected void EntityDataSource_Inserted(object sender, DetailsViewInsertedEventArgs e)

Upvotes: 2

Views: 2834

Answers (3)

alkos333
alkos333

Reputation: 641

DetailsViewInsertedEventArgs has a property called Values, but when you insert from DetailsView, you don't provide a primary key via textbox usually, so that newly assigned primary key won't be in there.

You could use EntityDataSource.Inserted event instead. It passes EntityDataSourceChangedEventArgs which has an Entity property which you can type-cast and then get the value of the primary key attribute. For example, if I had an entity called Dependent that I just inserted into my ObjectContext through the EntityDataSource, my event handler for the EntityDataSource could look like this:

protected override dependentInformationDataSource_OnInserted(object sender, EntityDataSourceChangedEventArgs e )
{
    // let's say my Dependent entity's primary key is called DependentKey
    int newPrimaryKey = ((Dependent)e.Entity).DependentKey;

    // do something with newPrimaryKey
}

Upvotes: 3

Josh Darnell
Josh Darnell

Reputation: 11433

You could pull the new ID directly from the database (I'm assuming SQL, but whichever database you're using) now that it has been inserted:

// an int to store your newly inserted ID
int newID = 0; 
// a string containing a query that gets your latest, inserted record.  
// Modify to your specific needs =)
string newIDQuery = "SELECT TOP 1 ID FROM tableName ORDER BY ID DESC;";
SqlCommand getNewIdCommand = New SqlCommand(newIDQuery, New SqlConnection("You Connection String"));
getNewIdCommand.Connection.Open(); // open the SQL connection
newID = getNewIdCommand.ExecuteScalar(); // this loads the int variable with the newest ID
getNewIdCommand.Connection.Close(); // close the SQL connection

Note: This example assumes your primary key column is an auto-increment-style integer field named ID. Modify the above code to suit your needs, or let me know if you have questions!

Upvotes: 0

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Exact syntax depends on the database, but usual way is to do somnething like in sql server

Insert MyTable()...) Values(...) Select Scope_Identity

So just execute the above through a reader, or an sp (you could return it as scalar then).

How and where you pass it back is up to you, but if you are passing in a viewmodel instance with the other data, populating it's id property is nice and clean.

If you have triggers and default constraints etc reloading the instance using the passed back identity is better.

Upvotes: 0

Related Questions