alaplume
asked on
ado recordset identity value not being returned
If I run the following code in VB I get the value of the Identity back in the recordset.
If I run the equivalent code in ASP, the value comes back empty.
Private Sub Command1_Click()
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = CreateObject("ADODB.Record Set")
Set Conn = CreateObject("ADODB.Connec tion")
rsProjectInfo.CursorLocati on = adUseClient
rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx' ;Data Source=IntranetDev", adOpenStatic, adLockBatchOptimistic
rsProjectInfo.AddNew
rsProjectInfo("ClientInfoI D") = 1 '(ClientInfoID)
rsProjectInfo("BusinessSol ution") = "test"
rsProjectInfo.UpdateBatch
Debug.Print rsProjectInfo("ProjectInfo ID")
End Sub
If I run the equivalent code in ASP, the value comes back empty.
Private Sub Command1_Click()
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = CreateObject("ADODB.Record
Set Conn = CreateObject("ADODB.Connec
rsProjectInfo.CursorLocati
rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx'
rsProjectInfo.AddNew
rsProjectInfo("ClientInfoI
rsProjectInfo("BusinessSol
rsProjectInfo.UpdateBatch
Debug.Print rsProjectInfo("ProjectInfo
End Sub
Try explicitly stating the fields you want to select in your sql statement. In this case, use:
SELECT ClientInfoID, BusinessSolution
FROM cpd_ProjectInfo
WHERE ClienInfoID=-1
SELECT ClientInfoID, BusinessSolution
FROM cpd_ProjectInfo
WHERE ClienInfoID=-1
ASKER
stiemark: rsProjectInfo("ProjectInfo ID") is the identity field. The other fields don't really matter.
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
ASKER
stiemark: rsProjectInfo("ProjectInfo ID") is the identity field. The other fields don't really matter.
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
ASKER
stiemark: rsProjectInfo("ProjectInfo ID") is the identity field. The other fields don't really matter.
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
kraig: I want to know why the exact same code doesn't work in ASP vs straight VB!
Don't ask me.
I didn't make up asp, I just try to make it work for what I need it to do.
I've had this issue before, but don't remember exactly how I got around it...
Good luck.
I didn't make up asp, I just try to make it work for what I need it to do.
I've had this issue before, but don't remember exactly how I got around it...
Good luck.
alaplume,
>>>rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx' ;Data Source=IntranetDev", adOpenStatic, adLockBatchOptimistic<<<
this connection string won't work in ASP. You need something like:
========================== ========== ========== =====
strConn = "Provider=SQLOLEDB.1;" _
& "Server=SQLServerMachineNa meOrIP;" _
& "Database=YourDBName;"_
& "uid=UserName;" _
& "pwd=Password;"
SET Conn = Server.CreateObject("ADODB .Connectio n")
Conn.Open strConn
Set rsProjectInfo = CreateObject("ADODB.Record Set")
rsProjectInfo.CursorLocati on = adUseClient
rsProjectInfo.Open sSQL, Conn, adOpenStatic, adLockBatchOptimistic
========================== ========== ========== ========
If this doesn't help, please post your ASP code.
Best Regards,
>apollois<
>>>rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx'
this connection string won't work in ASP. You need something like:
==========================
strConn = "Provider=SQLOLEDB.1;" _
& "Server=SQLServerMachineNa
& "Database=YourDBName;"_
& "uid=UserName;" _
& "pwd=Password;"
SET Conn = Server.CreateObject("ADODB
Conn.Open strConn
Set rsProjectInfo = CreateObject("ADODB.Record
rsProjectInfo.CursorLocati
rsProjectInfo.Open sSQL, Conn, adOpenStatic, adLockBatchOptimistic
==========================
If this doesn't help, please post your ASP code.
Best Regards,
>apollois<
ASKER
apollois: assume the connection string is ok.
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = Server.CreateObject("ADODB .RecordSet ")
rsProjectInfo.CursorLocati on = adUseClient
rsProjectInfo.Open sSQL, DBConnString, adOpenStatic,
adLockBatchOptimistic
rsProjectInfo.AddNew()
rsProjectInfo("ClientInfoI D") = 1
rsProjectInfo("BusinessSol ution") = "test"
rsProjectInfo.UpdateBatch( )
response.write("<br>" & isEmpty(rsProjectInfo("Pro jectInfoID ")))
response.write(":ProjectIn foID<br>")
End Function
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = Server.CreateObject("ADODB
rsProjectInfo.CursorLocati
rsProjectInfo.Open sSQL, DBConnString, adOpenStatic,
adLockBatchOptimistic
rsProjectInfo.AddNew()
rsProjectInfo("ClientInfoI
rsProjectInfo("BusinessSol
rsProjectInfo.UpdateBatch(
response.write("<br>" & isEmpty(rsProjectInfo("Pro
response.write(":ProjectIn
End Function
Replace
rsProjectInfo.UpdateBatch( )
WITH
rsProjectInfo.Update
IF NOT rsProjectInfo.EOF THEN
response.write "<BR>New ProjectInfoID is: " & rsProjectInfo("ProjectInfo ID") & "<BR>"
ELSE
Response.write "<BR> Unable to ADD new Record<BR>"
END IF
This will work if you connection string is good, and you have established a connection to the DB, and provided that "ProjectInfoID" is an IDENTITY field
rsProjectInfo.UpdateBatch(
WITH
rsProjectInfo.Update
IF NOT rsProjectInfo.EOF THEN
response.write "<BR>New ProjectInfoID is: " & rsProjectInfo("ProjectInfo
ELSE
Response.write "<BR> Unable to ADD new Record<BR>"
END IF
This will work if you connection string is good, and you have established a connection to the DB, and provided that "ProjectInfoID" is an IDENTITY field
here is what your code should be in ASP:
<%
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = Server.CreateObject("ADODB .RecordSet ")
Set Conn = Server.CreateObject("ADODB .Connectio n")
rsProjectInfo.ActiveConnec tion=Conn
rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx' ;Data Source=IntranetDev", adOpenStatic, adLockBatchOptimistic
if rsProjectInfo.EOF=False AND rsProjectInfo.BOF=False then
rsProjectInfo.MoveLast
end if
rsProjectInfo.AddNew
rsProjectInfo("ClientInfoI D") = 1 '(ClientInfoID)
rsProjectInfo("BusinessSol ution") = "test"
rsProjectInfo.Update
rsProjectInfo.Close
rsProjectInfo.Open "SELECT * FROM cpd_ProjectInfo WHERE CLientInfoID=1"
response.write rsProjectInfo("ProjectInfo ID")
%>
Hope this helps
MaxOvrdrv2
<%
sSQL = "SELECT * FROM cpd_ProjectInfo WHERE 0=1"
Set rsProjectInfo = Server.CreateObject("ADODB
Set Conn = Server.CreateObject("ADODB
rsProjectInfo.ActiveConnec
rsProjectInfo.Open sSQL, "User ID=iuser;password='xxxxxx'
if rsProjectInfo.EOF=False AND rsProjectInfo.BOF=False then
rsProjectInfo.MoveLast
end if
rsProjectInfo.AddNew
rsProjectInfo("ClientInfoI
rsProjectInfo("BusinessSol
rsProjectInfo.Update
rsProjectInfo.Close
rsProjectInfo.Open "SELECT * FROM cpd_ProjectInfo WHERE CLientInfoID=1"
response.write rsProjectInfo("ProjectInfo
%>
Hope this helps
MaxOvrdrv2
If you can use server-side cursors (like I do) you can try changing the CursorLocation property to "adUseServer" and the cursor type to "adOpenKeyset" (from "adOpenStatic").
Hope this helps.
Hope this helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
apollo... well... from the way he is adding his record (.addnew ("ClientInfo")=1 .update)...and since all he wants is a way to see how it works in ASP... i assumed that checking for 1 would be the best idea... and i mean... as far as i see.. the @@identity only works on MS SQL databases...
anyways...
my code still works... :)
MaxOvrdrv2
anyways...
my code still works... :)
MaxOvrdrv2
MaxOvrdrv2,
I didn't intend to start a debate here, but your code will only work if the ClientInifo is a unique field.
Both Oracle and MySQL have techniques to get the PK of the record just inserted.
IAC, using ADO with Recordset.Update should work for any DB. This ensures that you are getting the correct PK without having do issue any additional SQL statements.
Best Regards,
>apollois<
I didn't intend to start a debate here, but your code will only work if the ClientInifo is a unique field.
Both Oracle and MySQL have techniques to get the PK of the record just inserted.
IAC, using ADO with Recordset.Update should work for any DB. This ensures that you are getting the correct PK without having do issue any additional SQL statements.
Best Regards,
>apollois<
i know... and i agree... i was just providing an example using the same code that he posted originally... which... upon looking at it... was simply a test... to see if he could retreive the right record... later on... he could easily replace the 1 with a dynamic variable/value...
but yes... i do agree with you... there are many ways to get the last entered record in a DB... i was just basing myself on his example...
i didn't mean to be mean or anything... i was just defending my basis... :)
MaxOvrdrv2
but yes... i do agree with you... there are many ways to get the last entered record in a DB... i was just basing myself on his example...
i didn't mean to be mean or anything... i was just defending my basis... :)
MaxOvrdrv2
ASKER
I quit on trying to get the value back the way I wanted. The next best way was to use the @@IDENTITY.
rsProjectInfo("ClientInfoI
rather than
rsProjectInfo("ProjectInfo
?
Mark