ASP/MSSQL - getting auto-increment value of new record

  • stevegmag
  • Novice
  • Novice
  • User avatar
  • Joined: Jun 11, 2004
  • Posts: 29
  • Loc: Washington DC
  • Status: Offline

Post July 6th, 2004, 12:02 pm

I've seen this function used on 100s of tutorial sites, it works great on my DEV server but not my client's server.... is there a setting that would cause UPDATE not to preform as intended? The information is being added to the DB, but the ID is not being returned which is throwig off the rest of the script(s) that need this ID.

'strAutoFieldName = the field that auto-increments (ID is this case)


Code: [ Select ]
Function InsertRecord(tblName, strAutoFieldName, ArrFlds, ArrValues )
dim conn, rs, thisID
Set conn = Server.CreateObject ("ADODB.Connection")
Set rs = Server.CreateObject ("ADODB.Recordset")

conn.open dbConnectionString
conn.BeginTrans
rs.Open tblName, conn, adOpenKeyset, adLockOptimistic, adCmdTable
[b]
rs.AddNew ArrFlds, ArrValues
rs.Update
thisID = rs(strAutoFieldName)
response.write "thisID: " & thisID &"<br>"
' not getting the ID[/b]
rs.Close
Set rs = Nothing

conn.CommitTrans
conn.close
Set conn = Nothing

If Err.number = 0 Then
InsertRecord = thisID
End If
End Function
  1. Function InsertRecord(tblName, strAutoFieldName, ArrFlds, ArrValues )
  2. dim conn, rs, thisID
  3. Set conn = Server.CreateObject ("ADODB.Connection")
  4. Set rs = Server.CreateObject ("ADODB.Recordset")
  5. conn.open dbConnectionString
  6. conn.BeginTrans
  7. rs.Open tblName, conn, adOpenKeyset, adLockOptimistic, adCmdTable
  8. [b]
  9. rs.AddNew ArrFlds, ArrValues
  10. rs.Update
  11. thisID = rs(strAutoFieldName)
  12. response.write "thisID: " & thisID &"<br>"
  13. ' not getting the ID[/b]
  14. rs.Close
  15. Set rs = Nothing
  16. conn.CommitTrans
  17. conn.close
  18. Set conn = Nothing
  19. If Err.number = 0 Then
  20. InsertRecord = thisID
  21. End If
  22. End Function
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 6th, 2004, 12:02 pm

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 8th, 2004, 7:50 am

After you have execeuted the insert query try this SQL query

Code: [ Select ]
SELECT IDENT_CURRENT('yourTableName') AS C_ID


should return the newest value from your auto_inc field. (inside Microsoft SQL)
Watch me grow
  • stevegmag
  • Novice
  • Novice
  • User avatar
  • Joined: Jun 11, 2004
  • Posts: 29
  • Loc: Washington DC
  • Status: Offline

Post July 8th, 2004, 7:52 am

I added a select max(id) and it works... hopefully more than one entry at a time won't occur :D
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 8th, 2004, 7:58 am

Yeah that is the way I would have done it with php or used the mysql_insert_id call
Watch me grow

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: ScottG and 206 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.