Solved

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

Posted on 2008-10-28
4
172 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
[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
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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