Hashem AL-Rifai
Hashem AL-Rifai

Reputation: 173

How could I get the new ID of an inserted record in ASP.NET DetailsView using LinqDataSource

I've got the answer if I were using SqlDataSource by Overriding the Insert Statement and use the @@IDENTITY property, but when I use the LinqDataSource, I couldn't override the Insert statement!

so, the problem is:

I'm using ASP.NET Webform, DetailsViewControl in Insert Mode and LinqDataSource.

I want to retrieve the new ID of the inserted record. What's the easiest and the most efficient way to do that?

Thanks.

Upvotes: 0

Views: 1088

Answers (1)

amit_g
amit_g

Reputation: 31260

Using LinqDataSource.Inserted Event

Example from MSDN shows that newProduct.ProductID is the new ID.

protected void LinqDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
    if (e.Exception == null)
    {
        Product newProduct = (Product)e.Result;

        // newProduct.ProductID is the new ID
    }
    else
    {
        // Some Exception - e.Exception.Message;
    }
}

Upvotes: 2

Related Questions