none
Retrieving record ID after insert? RRS feed

  • Question

  • So, I'm hoping I haven't worn out my welcome asking novice questions, hopefully there are som many other noobs that I blend in ;)

    I am doing an insert of a new record, which has an auto incremented identity in the table.  I'd like to read that back so that I can include it in query string that I send the person that performed the insert to retrieve the record later (an email notification basically).  I hope that I'm explaining this clearly enough. 

    Would anyone be kind enough to point me in the right direction?  Thanks as always for all the gracious help I've received here...

    Paul

    Friday, March 9, 2012 12:16 AM

Answers

  • Try the below code, it will solve your problem:

    using (YourEntity Eobj = new YourEntity())
    {
    	Table1 tblObj = new Table1();
    	tblObj.column1 = value1;
    	tblObj.column2 = value2;
    	Eobj.AddToTable1(tblObj);
    	Eobj.SaveChanges();
    	int i = tblObj.your_auto_id_column;
    }

    Friday, March 9, 2012 5:02 AM

All replies

  • Try the below code, it will solve your problem:

    using (YourEntity Eobj = new YourEntity())
    {
    	Table1 tblObj = new Table1();
    	tblObj.column1 = value1;
    	tblObj.column2 = value2;
    	Eobj.AddToTable1(tblObj);
    	Eobj.SaveChanges();
    	int i = tblObj.your_auto_id_column;
    }

    Friday, March 9, 2012 5:02 AM
  • Thanks very much!  As soon as I saw that I said "why didn't I think of that!"  I'll plug it in this morning and report back...

    I'm very grateful for the help!

    Friday, March 9, 2012 11:29 AM
  • Worked like a charm, thank you so much.  I had assumed that once I did the submit that the new object and the references to it were gone.  But when you assumeeeeeeeeeeee ;)

    Thanks again!  You made my morning...

    Friday, March 9, 2012 2:53 PM