Solved

Connection objects and last_insert_id() help !

Posted on 2004-04-17
3
405 Views
Last Modified: 2012-05-04
Hey everyone,

I'm ripping my hair out over this problem:

I have a Insert statement, and i want to retrieve the last_insert_ID() after it has executed so it goes like this:

SQLINS = "INSERT INTO family_albums (category,fam_ID,album_name,hidden,system_album,in_progress,user_ID) values(null,'"&request.cookies("access")("family_ID")&"','"&Upload.Form("Albumselect")&Upload.Form("Albumcreate")&"',1,0,1,"&request.cookies("access")("user_ID")&")"
objCmd.CommandText = sqlins
objCmd.Execute
RS1.open "SELECT LAST_INSERT_ID() AS `album_ID`" , "DSN=xxxx; uid=xxxx;pwd=xxxx"
session("varalbum_ID") = RS1("album_ID")
RS1.close

That returns a RS1("album_ID") of 0.

HOWEVER, when i do
SQLINS = "INSERT INTO family_photos (fam_ID,addedby_user,album_ID,file_name) values("&request.cookies("access")("family_ID")&","&request.cookies("access")("user_ID")&",LAST_INSERT_ID(),'"&file.ExtractfileName&"')"
objCmd.CommandText = sqlins
objCmd.Execute

the last_insert_ID() works (HOWEVER, I can't use it because it is in a loop and would increment itself (when i dont want it to)

I think i've answered my own question.. but i still dont know how to make it work.
I'm asuming the problem is that i'm opening a whole new connection object and it's not finding any last inserted id's.

So my question if the above is right, how would i do a "SELECT" on a objcmd and return a value in a RS("") fashion ?

Thanks for any insight!\

Chris
0
Comment
Question by:garfield1979
3 Comments
 

Accepted Solution

by:
jameswalt earned 200 total points
ID: 10850723
I've had this problem many times. Since an insert does not have any return values, we could always execute a query such as follows:
This is the main problem with using autoincrements as primary keys (that and the additional storage required)

CONNECT.EXECUTE INSERT STATEMENT
RS1.OPEN "SELECT Max(album_ID) AS lastID FROM family_albums"

Response.write RS1("lastID")


I hope this helps - it's pretty much what you supposed, but a more clear solution.
0
 

Author Comment

by:garfield1979
ID: 10850752
Thanks James.

That solution wil work, i'm at nerves end and i'm about to throw this system against the wall..

:-D

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10851342
The main problem with this approach is performance and in a multi-user environment you could pick up the wrong value.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Classic ASP/VBScript/ADO MSSQL Insert failure with parameters 4 21
EOF BOF error classic asp 8 43
asp syntax 3 37
ASP SQL Syntax Duplicate Key 7 92
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now