Solved

Auto Increment primary key issue

Posted on 2006-06-28
1
230 Views
Last Modified: 2010-04-23
Hi,
I have a sql server database, and for a primary key I use an auto incrementing integer field.  In my code I create a new row like this...

Dim rowNewRow As DataRow
rowNewRow = Me.DsTheDataSet.TheTable.NewRow
rowNewRow.Item("Field1") = "something"
Me.DsTheDataSet.TheTable.Rows.Add(rowNewRow)

That works fine, but the primary key created for the new row isn't the real key, it's just an auto increment starting from 1.  Once the data is save the primary key updates to the real one.  The thing is I want to be able to use the primary key value before saving the dataset.  Is this possible?

Thanks a lot
0
Comment
Question by:hmcnasty
1 Comment
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 500 total points
ID: 17001787
I can see several possiblities, but none of which are very safe.  First, you can use IDENT_CURRENT('tablename') to get the current identity value...so IDENT_CURRENT('tablename') + IncrementValue [usually 1] would be the NEXT primary key that will be inserted.  You can use this knowledge in tandem with your row creation, but it's a very very very bad idea if someone else could be adding rows at the same time, in which case you would end up with spurious records.

Another method would be to write a stored procedure that inserts your new row and returns the PK.  After you get the PK, you can assign it to the row in the dataset and go about your business.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now