Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Increment primary key issue

Posted on 2006-06-28
1
Medium Priority
?
239 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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