maybe FCTable.Refresh after the post. be carefull by the way if the autoincrement field is the index you are using. it might be best to use a diff index
also see:
http://bdn.borland.com/art
Main Topics
Browse All TopicsI am using SQL Server 2005 linked to ADO datasets in Delphi 2006.
I have a table [FCTable] which contains a field [FC_FCRef] which is defined as an IDENTITY field in the sql (auto incremented by the server itself).
I have a piece of code which appends a record to FCTable, sets some fields in the record then posts it. This works fine and when I look at the resultant record in SQL Server manager I can see that the identify field FC_FCRef has been set to a value by the server. The problem is that the ADO data set doesn't see the true value of FC_FCRef and believes it still to be Zero as it's the server side that's set it not the code. This means when the next lines of code try to save the FC_FCRef somewhere it's saving the wrong value.
If I close the open the table the values are re-read and are correct but obviously that's highly inefficient when I'm updating hundreds of records. If I set the dataset CursorLocation to clUseServer then it also works correctly but using server side cursors is not recommended as it's (apparently) very inefficient.
Is there any simple way I can get the ADO dataset to get the correct value for the field?
For your reference the Delphi code is;
{ Create a blank claim record }
FCTable.Append;
{ Complete the record fields }
FCTableFC_Date.AsString := DateToStr(Now());
.
.
.
{ Post the claim record }
FCTable.Post;
{ Update the census record with the claim reference }
CPTable.Edit;
CPTableCP_FCRef.AsLargeInt
CPTable.Post;
Using this code the CP_FCRef simply gets set to zero every time even though the actual record in the FCTable is showing a unique number of FC_FCRef :-(
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
maybe FCTable.Refresh after the post. be carefull by the way if the autoincrement field is the index you are using. it might be best to use a diff index
also see:
http://bdn.borland.com/art
Ok, the requery method suggested by kretzschmar works but reading the Borland documentation on it apparently its just the same as closing and opening the database except it maintains cursor location.
As I indicated in my original question "If I close the open the table the values are re-read and are correct but obviously that's highly inefficient when I'm updating hundreds of records" (not sure why calinutz re-suggested it) so if that the only way to do it or is there a more efficient way?
Thanks.
Yes, I've tried it in a TADOQuery as that's the only way I would know how to do a direct SQL command.
Perhaps this is all because I'm using ADO rather than dbExpress? If dbExpress is a better solution (more efficient) then I'm happy to consider moving to that instead of ADO as long as I can get record selection in grids to work properly as when I tried it before and set the RowCurrentColor of the grid to blue, ALL rows display blue regardless of what I do to the cursor!
Perhaps I should have stuck to good old BDE ... lol :-))
Meikl, the interest in getting back the autogenerated value is for auditing. The FCTable records are finance claims generated from census records in the CPTable so for traceability once the FCTable record is generated I write back the number to the CPTable.
It is done on a "Batch" basis with each batch containing 50 to 100 records however this is a national web based system and we could have up to fifty people at once doing this batch process and potentially thousands of other people updating other records so I need to be very efficient when I'm hitting the SQL server.
well, ok, currenlty i have never used d2006,
so i don't know what is changed there.
my last release i have coded with was d7
i see two possibilities (if possible)
- use a trigger on your targettable to perform the auditing
- use a stored procedure which returns the id so that you can supply it for your insert
(don't know if this is possible with SQLServer, my focus is more on oracle)
meikl ;-)
I'm not an SQL server expert. I know you can have both triggers and stored procedures on SQL server but I would not know how to make them properly or how to use them to link back to Delphi.
I have made a stored procedure once in my life, so do a complex calculation for returning to another column not to Delphi. I've never used triggers and know nothing about them.
It looks like there is no easy solution for this problem :-(
I don't have an alternative; I must use SQL server as it's our company standard.
It looks like there is no simple solution to this problem so I have decided to change the way my app works and instead of using an autoincrement field I'll just store a number elsewhere and increment manually in the app each time I write a record. It's not as elegant as getting the server to do it but it will solve the problem.
As a test I filled out my tables to the size we expect in a year's time ... and now I know what they mean about Serverside cursors being inefficient! I gave up waiting after my app spent 5 minutes flogging the SQL server just starting up with a few Serverside cursors! I changed to clientside cursors and although it took a while to start (10 to 15 seconds) it was then useable.
I've used the requery method on these large tables and although it's not fast it's just about acceptable, so I'm giving the points to kretzschmar.
It's not the simple solution I was looking for but it looks like the best I'm going to get and I need to move on with the app now. Thanks for all your suggestiong guys.
Chris
Geo,
I have tried reading the @@IDENTITY and it doesn't work! It always returns nothing. I've tried it by setting up a TADOQuery linked to the same TADOConnection. the queries SQL reads "select @@identity as ident". The query is set active immediately after the connection is enabled. I've then created the following function;
Function TIWUserSession.IdentityQue
begin
{ Query server for latest ID }
IdentQuery.Requery();
IdentityQuery := IdentQueryident.AsString;
end;
One would then expect after a TADOTable.post a call to IdentityQuery() would return the identity from the table. It actually returns null.
Business Accounts
Answer for Membership
by: mokulePosted on 2006-08-09 at 11:07:53ID: 17281505
Maybe try to set teValue := arAutoInc;
CPTableCP_FCRef.AutoGenera