Solved

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

Posted on 2008-10-28
4
173 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 42
ASP.NET Complete Life Cycle for all the controls 2 39
Web page design problem 3 37
Textbox autocomplete using jquery in asp.net 13 42
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

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