Forums

This topic is locked

ID of last row inserted?

Posted 17 Aug 2004 23:45:59
1
has voted
17 Aug 2004 23:45:59 Phil Shackleton posted:
Can anyone tell me how to get the ID of the last row inserted when using auto_increment?

Replies

Replied 23 Aug 2004 19:12:13
23 Aug 2004 19:12:13 Carl Grint replied:
Try this, it works for Access and MSSQL server

Comment out the following 2 lines of the INSERT behaviour

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
' MM_editCmd.ActiveConnection.Close <!--comment this line out -->

If (MM_editRedirectUrl <> "" Then
' Response.Redirect(MM_editRedirectUrl) <!--comment this line out -->
End If
End If

End If
%>


Add a min recordset below the close of the INSERT behaviour


<%
If (CStr(Request("MM_insert") <> "" Then <!-- if normal insert -->
If (CStr(UploadFormRequest("MM_insert") <> "" Then <!-- if using asp uploader on page -->
set tutorialsRS = Server.CreateObject("ADODB.Recordset"
recordsetname.ActiveConnection = MM_editCmd.ActiveConnection
recordsetname.Source = "SELECT max(ID) as MaxID FROM table"
recordsetname.CursorType = 0
recordsetname.CursorLocation = 2
recordsetname.LockType = 3
recordsetname.Open()
Session("LatestID"=recordsetname("MaxID"
Response.Redirect(MM_editRedirectUrl)
end if
%>


ID is the name of the column you use for the records ID, change as required, also change "recordsetname" to the name of the whatever you want to call this recordset, obviously making sure it does not duplicate any recordset used on the page.
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Can anyone tell me how to get the ID of the last row inserted when using auto_increment?

<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Web Development Consultant
providing web development services including Web site design and development, the complete development of Intranets/Extranets and a wide range of Web applications.
Details can be viewed at www.cgw3.co.uk

Edited by - CGrint on 23 Aug 2004 19:15:01

Reply to this topic