8 Replies - 435 Views - Last Post: 17 March 2011 - 04:38 PM Rate Topic: -----

#1 blodzoom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-March 11

Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 11:27 AM

I've got this code:
  pers.FirstName = lblFirstName.Text;
        pers.LastName = lblLastName.Text;
        dc.Persons.Insertonsubmit(pers);


Which inserts a new person to my database, which assigns a PersonKey (primary key) to that person.
In order to insert the next bit of information for the person into a different table, I need to know what his PersonKey is. How can I get that?
Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving key after inserting into SQL using LINQ

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 11:31 AM

The key should be updated on the pers object after the insert. So whatever your key name is, it should be a property of pers, and after the insert, it will be populated.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 12:15 PM

So assuming your Key column is called "PersonID", then your code would look like...

pers.FirstName = lblFirstName.Text;
pers.LastName = lblLastName.Text;
dc.Persons.Insertonsubmit(pers);

// you forgot this...
dc.SubmitChanges();

int personID = pers.PersonID;



The Person object is automatically updated with the auto-generated identity.
Was This Post Helpful? 0
  • +
  • -

#4 blodzoom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-March 11

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 01:41 PM

Thank you.

As per usual, I was making it more complicated than it was. I was also trying to do the dc.submitchanges() at the very end and do all updates together, which obviously wouldn't work.

This forum is awesome. Hope I can contribute someday.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 01:51 PM

You should be able to do all updates together...Something like this?

Person p1 = new Person();
p1.FirstName = "John";
p1.LastName = "Doe";

Person p2 = new Person();
p2.FirstName = "Jane";
p2.LastName = "Doe";

db.Persons.Insertonsubmit(p1);
db.Persons.Insertonsubmit(p2);

db.SubmitChanges();

int p1ID = p1.PersonID;
int p2ID = p2.PersonID;



That should work fine.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 02:13 PM

Well, if you're planning on using that retrieved ID in a future insert/update, of course you'd have to submit twice. It doesn't know what the ID is going to be until it's gone to the DB and come back.
Was This Post Helpful? 0
  • +
  • -

#7 blodzoom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-March 11

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 03:34 PM

Thanks to you guys, I finished the assignment. Just one more to go and that's it for the semester.

Programming is pretty frustrating. I bang my head against the wall getting pissed because I think I don't know how to do something but then it turns out I forgot about capitalization or some stupid thing.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 03:37 PM

It's all part of the game.

First things first, trust Visual Studio. If it's auto-correcting your variable name into something else, it means it can't find what you're trying to type, and that means it either doesn't exist, or you have a syntax error preventing it from being parsed.

Second, sometimes you just have to walk away for a few minutes. Things don't always work the first way you come up with. In fact, they rarely do.

Third, it's awesome that you're in a class that's teaching you LINQ to SQL. That's pretty new stuff, and pretty "out there" all things considered.
Was This Post Helpful? 0
  • +
  • -

#9 blodzoom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-March 11

Re: Retrieving key after inserting into SQL using LINQ

Posted 17 March 2011 - 04:38 PM

View PostinsertAlias, on 17 March 2011 - 03:37 PM, said:

It's all part of the game.

First things first, trust Visual Studio. If it's auto-correcting your variable name into something else, it means it can't find what you're trying to type, and that means it either doesn't exist, or you have a syntax error preventing it from being parsed.

Second, sometimes you just have to walk away for a few minutes. Things don't always work the first way you come up with. In fact, they rarely do.

Third, it's awesome that you're in a class that's teaching you LINQ to SQL. That's pretty new stuff, and pretty "out there" all things considered.


Thanks for the tips. I think the first one, in particular, is useful, I think I have a tendency to "push" past the intellisense when I think I'm on a roll. I should slow down and pay more attention.

I'm in the DBA program at Seattle Central Community College. The equipment there is pretty impressive (I believe Microsoft donated the machines) and most of the teachers seem to be really good.

I'm off to attempt the final project, you can probably expect more noob questions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1