Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Get ID from a DB Field after INSERT
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Get ID from a DB Field after INSERT

 Post Reply Post Reply Page  12>
Author
d088 View Drop Down
Newbie
Newbie


Joined: 02 December 2004
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote d088 Quote  Post ReplyReply Direct Link To This Post Topic: Get ID from a DB Field after INSERT
    Posted: 02 December 2004 at 2:04pm
I am using a form on an ASP page to submit to a database.  The database creates the record and assigns the pk_ID field.
I need to get the value of that field so that if the user pays online, I can pass it to VeriSign.

Any help would be appreciated.

Thanks,
Ben
Back to Top
dj air View Drop Down
Senior Member
Senior Member
Avatar

Joined: 05 April 2002
Location: United Kingdom
Status: Offline
Points: 3627
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj air Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 2:13pm
what code are you using to add the record. if its not a ADD into query but a Select ...etc..

Connection.Addnew

if the above line is used.

you can use the following after its updated

connection.requery

connection.movelast

ID = connection("PK_ID")

thats not actual code but is a way of doing so

you will neded to set the connection.locktype = 3
Back to Top
d088 View Drop Down
Newbie
Newbie


Joined: 02 December 2004
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote d088 Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 2:17pm
Thank you - I will try this out.  Here is the code if you are interested:


'INSERT FORM INFORMATION INTO DATABASE

If request.form("FName")<>"" Then FName= request.form("FName") Else FName= "None"
If request.form("LName")<>"" Then LName=request.form("LName") Else LName="None"
If request.form("Company")<>"" Then Company=request.form("Company") Else Company="None"
If request.form("Phone")<>"" Then Phone=request.form("Phone") Else Phone="None"
Fax="NULL"
If request.form("Email")<>"" Then Email=request.form("Email") Else Email="None"
If request.form("Address1")<>"" Then Address1=request.form("Address1") Else Address1="None"
If request.form("Address2")<>"" Then Address2=request.form("Address2") Else Address2="None"
If request.form("City")<>"" Then City=request.form("City") Else City="None"
If request.form("State")<>"" Then St=request.form("State") Else St="None"
If request.form("Zip")<>"" Then Zip=request.form("Zip") Else Zip="None"
If request.form("Cancellation_Agreement")<>"" Then Cancellation_Agreement=request.form("Cancellation_Agre ement") Else Cancellation_Agreement=None
If request.form("Amount_Due")<>"" Then Amount_Due=request.form("Amount_Due") Else Amount_Due="None"
If request.form("IP")<>"" Then IP=request.form("IP") Else IP="None"

'OPEN DATABASE CONNECTION
myDSN="DSN=hp04"
set connAuction=server.createobject("adodb.connection")
connAuction.open myDSN

sql="INSERT INTO Primary_Registrations (FName, LName, Company, Position, Phone, Fax, Email, Address1, Address2, City, St, Zip, Cancellation_Agreement, Amount_Due, IP) VALUES ('"&FName&"', '"&LName&"', '"&Company&"', '"&Position&"', '"&Phone&"', '"&Fax&"', '"&Email&"', '"&Address1&"', '"&Address2&"', '"&City&"', '"&St&"', '"&Zip&"', '"&Cancellation_Agreement&"', '"&Amount_Due&"', '"&IP&"')"
connAuction.execute(sql)


Back to Top
dj air View Drop Down
Senior Member
Senior Member
Avatar

Joined: 05 April 2002
Location: United Kingdom
Status: Offline
Points: 3627
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj air Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 2:34pm
you can't use the code i said with that type of SQL

you will nedd to set another query that
SELECT TOP 1 Primary_Registrations.PK_ID FROM Primary_Registrations ORDER BY Primary_Registrations DESC;

that SQL will give you the last record in the DB.

then you can bring in the value
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 3:26pm
Thus in the unlikely event of two users buying something at the same time, the Select Top1 could return the wrong record. Not sure if you use access but in SQL Server You can put that stuff into a stored procedure with a return type of @@Identity so for example
INSERT INTO table1 (field1,field2,field3)
VALUES (1,12,125)
SELECT @@IDENTITY AS 'Identity'
would return the last ID.

If you are using Access Dead (not sure if something similar is possible) I would create a GUID in asp and use that as the PK in the table instead of an autonumber.
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 4:15pm
I couldn't figure it out with Access, so I did a select top 1 order by ID desc immediatly after the input.  Works fine as long as no one does an input in that nano second the script is executing.

This seems to be a problem with quite a few scripts that pops up every now and then.
Back to Top
d088 View Drop Down
Newbie
Newbie


Joined: 02 December 2004
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote d088 Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 4:28pm
I really appreciate the advice fellas.  This should work just fine for the volume that I'll be dealing with.
Thanks!
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 02 December 2004 at 4:35pm
Access (Jet) supports @@Identity as well:
http://support.microsoft.com/kb/q232144/
http://www.adopenstatic.com/experiments/fastestautonumber. asp

Cheers,
Andrew
There have been many, many posts made throughout the world...
This was one of them.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.