You are here: Experts > Computing/Technology > Business Software > Active Server Pages Programming (ASP) > Get inserted Record ID
Expert: Ashley Brazier - 3/4/2009
Question QUESTION: I will Get Right to it. I am using classic asp pages to develop my site. I am trying to query the database for a single value, and plug that value into a session variable.
the query I run is something like this. Select ID from from cars where cars = blue and year = 2009.
I need to know how to return the single value an place its value into a session variable
ANSWER: <%
Dim Conn
Dim SQLTemp
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring =
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Server.MapPath("\yourdb.mdb")& ";"
Conn.Open
SQLTemp = "SELECT * FROM yourTable"
set rstemp=Conn.execute(SQLTemp)
Do While Not rstemp.EOF
rstemp.Fields("whateverdatabasefield").Value
rstemp.MoveNext
Loop
Conn.Close
Set Conn = Nothing
%>
---------- FOLLOW-UP ----------
QUESTION: How can I Get the Id of a newly inserted record. The Id field is a auto number
Answer sql_insert = "set nocount on ; insert into reservas (turnier_id, firstname, lastname, xtel, xdni, xplz, xtown, email, startdate, startmonth, enddate, endmonth, PLZ, town, country, xtitel, xtime, extra, region, res1, res2, res3, res4 ) values("&turnier_id&", '"&firstname&"','"&lastname&"','"&xtel&"','"&xdni&"','"&xplz&"','"&xtown&"','"&email&"', "&startdate&","&startmonth&","&enddate&","&endmonth&",'"&PLZ&"','"&town&"','"&country&"','"&xtitel&"','"&xtime&"','"&extra&"','"®ion&"','"&res1&"','"&res2&"','"&res3&"','"&res4&"'); select scope_identity() id "
'response.write sql_insert
conn = StrAdminProvider
Set rs=Server.CreateObject("ADODB.Recordset")
rs.open sql_insert, conn
id_number=rs("id")
Add to this Answer Ask a Question