Solved

Auto Increment primary key issue

Posted on 2006-06-28
1
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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