dc

dc

  • NA
  • 663
  • 0

C# access to to sql server table after update the table

Aug 24 2012 7:44 PM
In a C# 2010 application, I am trying to determine how and/or when I can obtain the identity key that was inserted into a table for a report that needs to be generated.

  Basically in a linq to sql connection that connects to a sql server 2008 r2 database I have code that looks like the following:

db.Trans.InsertOnSubmit(trn);
db.Custs.InsertOnSubmit(cust);
db.SubmitChanges();
create_customer_report();

Basically in the line of code:
db.Trans.InsertOnSubmit(trn);

A new row is setup in the table called Trans. I do not believe the key to the table is actually updated in the database until the submitchanges() call is made.
In the line of code:

create_customer_report();

I need to add a line to the report in the create_customer_report method that includes the key to the table that was just inserted into the table from the code above.


I am concerned that I might not get the correct by doing a select max(key) from tran  table since other users could be updating the table almost simultaneously in a different thread.

Thus my question is how can I obtain the key to the Trans table for the row that was just inserted into the table and have it appear on the customer_report?