Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-28
4
Medium Priority
?
178 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 2000 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 60

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

877 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