By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Retrieve Identity Column

P: n/a
Hello,

I would need some help with this identity column problem in
SQLServer.

I have a database with two tables. The Parent table has an Identity
column.

Parent (ParentID IDENTITY, Name)
Child (ChildID, Name, ParentID)

In C#, when I try to create a Parent and Child info, I have a problem
with the identity column. How can I create a Parent row with many
child rows and then update the DataBase? Can I retreive the identity
column from the parent row?

I am also using a .xsd (DataModel) to make things easier when using
DataSets.

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

"WhiteEagl" <wh*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:3f**********@127.0.0.1...
Hello,

I would need some help with this identity column problem in
SQLServer.

I have a database with two tables. The Parent table has an Identity
column.

Parent (ParentID IDENTITY, Name)
Child (ChildID, Name, ParentID)

In C#, when I try to create a Parent and Child info, I have a problem
with the identity column. How can I create a Parent row with many
child rows and then update the DataBase? Can I retreive the identity
column from the parent row?


In dataset you set identity column AutoIncrement = true, AutoIncrementSeed
= -1 and AutoIncrementStep = -1
So it creates negative keys.
InsertCommand should have at least identitity column retrieve statament at
the end, something like:
INSERT INTO .... ; SELECT SCOPE_IDENTITY
So the datatable gets real identity value after database update.
Check out
HOW TO: Retrieve an Identity Value from a Newly Inserted Record from SQL
Server by Using Visual C# .NET
http://support.microsoft.com/default...oduct=NETFrame
Note - it has an error - it uses @@identity instead of scope_identity.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com
Nov 15 '05 #2

P: n/a
Thank you, I'll try this

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.