Active Server Pages Programming (ASP)/Get inserted Record ID

Advertisement


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")

Active Server Pages Programming (ASP)

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ashley Brazier

Expertise

Main focus is ASP, SQL Server 2000 & 2005

Experience

Over 6 years experience.

Education/Credentials
BCs Honours Software Design and Networks

©2012 About.com, a part of The New York Times Company. All rights reserved.