Solved

Getting the MAX primary key in order to add 1 for an INSERT statement

Posted on 2008-10-28
4
168 Views
Last Modified: 2010-04-14
How do I get the MAX primary key from my table and then add one to it for my INSERT statement.
I am using VB if needed.

In the code snippet that VALUE("1001"......  is where i am currently hard coding the next ID number.  I want to make it dynamic so that it changes to the next possible number.  I might have mutiple users trying to write at the same time.
<asp:SqlDataSource runat="server" id="SqlInsert" InsertCommand="INSERT INTO [IncidentTmp] ([ID], [PtAcct], [PtName], [PtStat], [PtLoc], [Event], [Facility], [EventLoc], [EventDept]) VALUES ('1001',@PtAcct, @PtName, @PtStat, @PtLoc, @drpdwnEvent, @drpdwnFacility, @drpdwnLocation, @drpdwnDept)"  ConnectionString="<%$ ConnectionStrings:QIMConnectionString %>" SelectCommand="SELECT [ID], [PtAcct], [PtName], [PtDob], [PtDos], [PtStat], [PtLoc] FROM [IncidentTmp]">

		<InsertParameters>

			<asp:parameter Name="PtAcct" Type="String" />

			<asp:parameter Name="PtName" Type="String" />

			<asp:parameter Name="PtStat" Type="String" />

			<asp:parameter Name="PtLoc" Type="String" />

			<asp:parameter Name="drpdwnEvent" Type="String"/>

			<asp:parameter Name="drpdwnFacility" Type="String"/>

			<asp:parameter Name="drpdwnLocation" Type="String"/>

			<asp:parameter Name="drpdwnDept" Type="String"/>

		</InsertParameters>

	</asp:SqlDataSource>

Open in new window

0
Comment
Question by:aninec
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
Blackninja2007 earned 500 total points
ID: 22820493
where not use sql integer Ident which will auto count and concurrent safe.
0
 
LVL 3

Expert Comment

by:Blackninja2007
ID: 22820501
.... [RecOrder] [int] IDENTITY(1,1) NOT NULL,
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22820585
Not sure if this is what was being suggested above, but I would leave out ID from the INSERT statement and let the database PK autonumber take care of it.  If your table structure doesn't currently autonumber, and you can change I would do so.

If you cannot, then I would suggest you use a stored procedure to do inserts and you can use transaction with lock as you must:

DECLARE @nextID INT
SELECT @nextID = ISNULL(MAX(ID), 0) + 1 FROM IncidentTmp

Then use the @nextID with other values to complete insert.

Otherwise, you will have to do application locking within your ASP.NET code to ensure that you don't try to insert same ID from multiple calls.
INSERT INTO [IncidentTmp] ([PtAcct], [PtName], [PtStat], [PtLoc], [Event], [Facility], [EventLoc], [EventDept]) VALUES (@PtAcct, @PtName, @PtStat, @PtLoc, @drpdwnEvent, @drpdwnFacility, @drpdwnLocation, @drpdwnDept)

Open in new window

0
 
LVL 3

Expert Comment

by:Blackninja2007
ID: 22820905
Sorry should have made myself clearer ...

I am suggesting adding an auto incrementing field to the table and using that. In the example I gave above i called in RecOrder ... sorry should have called it ID to save confusion.

sorry for the confusion

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

16 Experts available now in Live!

Get 1:1 Help Now