Results 1 to 6 of 6

Thread: ADODB / @@Identity problem.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671

    ADODB / @@Identity problem.

    The following code works fine on my PC (SQL 7.0) but errors when running on another Machine (with SQL 2000) installed.

    The tables should be the same on both machines so what else may be causing this error?

    VB Code:
    1. Set Conn = Server.CreateObject("ADODB.Connection")
    2. Query = "DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "');select @@identity"
    3. Conn.Open(Application("ConnectionString"))
    4. Set RS = Server.CreateObject("ADODB.RecordSet")
    5. Set RS = Conn.Execute (Query).NextRecordset
    6. JustInsertedID = RS(0)


    NB the error is "Item cannot be found in the collection corresponding to
    the requested name or ordinal"

    on the

    JustInsertedID = RS(0) line

  2. #2
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748
    Hi Martin,
    Does it help if you do "select @@identity as My_Identity" ?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Thanks, I'll give that a shot.

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Martin.

    It seems you have to run the query WITHOUT ASSIGNING IT TO A RECORDSET OBJECT and then run the SELECT @@IDENTITY query after it (not in the same step) similar to:

    objConn.Execute "UPDATE something SET somethingelse . ...."

    Set objRS = objConn.Execute("SELECT @@IDENTITY")

    if not objRS.eof then response.write (or debug.print) objRS.Fields(0)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You could also try setting the "Set NoCount" property of the query:
    VB Code:
    1. Set Conn = Server.CreateObject("ADODB.Connection")
    2. Query = "[b]Set NoCount On[/b] DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "') select @@identity"
    3. Conn.Open(Application("ConnectionString"))
    4. Set RS = Server.CreateObject("ADODB.RecordSet")
    5. Set RS = Conn.Execute (Query)
    6. JustInsertedID = RS(0)

    I also removed the semicolon before the select @@identity and the .NextRecordset.

    You can try and see if this works.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Thanks all of you, I ended up going with
    VB Code:
    1. Query = "SET NOCOUNT ON;DECLARE @Email varchar(255) SELECT @Email = (SELECT uemail FROM aamem WHERE ukey = " & IsContactedBy & ") INSERT INTO AAMem (UTitle, UFName, ULastC, UEmail, UCtry, URandomID, UPassword, Visits, UFNameEng, ULastCEng, IsContactedBy) VALUES ('" & UTitle & "','" & UFName & "','" & ULastC & "',@Email,'" & UCtry & "','" & URandomID & "','" & UPassword & "','" & Visits & "','" & UFNameEng & "','" & ULastCEng & "','" & IsContactedBy & "');SELECT @@IDENTITY AS NewID;"
    2.  
    3. Conn.Open(Application("ConnectionString"))
    4. Set RS = Server.CreateObject("ADODB.RecordSet")
    5. Set RS = Conn.Execute (Query)
    6. JustInsertedID = RS("NewID")

    and that seems to have done the trick. The bizarre thing is however that I am using my original technique in 2 other pages throughout the project (following bog standard inserts) and they seem to be performing as expected. I don't have access to the other server to do any testing on it (I am sending my customer the pages and they are putting them up on there and feeding back any problems). But is it possible that in SQL 2000 For some reason the DECLARE @Email varchar(255) may implicitly set no count on or in some other way affect the RecordSet(s) returned?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width