Solved

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

Posted on 2008-10-28
4
171 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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