Problem with SQL Server Computed Fields

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Mahmood_M
Posts: 20
Joined: Thu 24 Sep 2015 21:18

Problem with SQL Server Computed Fields

Post by Mahmood_M » Sat 11 Nov 2017 20:14

I have a problem with Computed Fields in SQL Server
Steps to reproduce the problem :
1. Create a new Table in SQL Server and add some fields to it
2. Create a field ( name it "MyField" ) and set a formula in the "Computed Column Specification" section of field properties like this : ([ID] + 1000) - ( ID is the auto-increment field )
3. Create a new project in Delphi, add a TUniConnection and a TUniTable
connect Table and add fields to it ( add a DBGrid to see the values )
4. Insert a record in the table :

Code: Select all

UniTable1.Insert;
UniTable1.FieldByName('FName').AsString := '';
UniTable1.FieldByName('FFamily').AsString := '';
...
UniTable1.Post;
* not assign any value to Computed field (MyField);
Now the problem is MyField has no value in DBGrid untile TUniTable reactivated !
If you try to Edit Table like this :

Code: Select all

UniTable1.Edit;
...
UniTable1.Post;
an error shown as "Record was changed by another user"

All Computed fields has this problem, what goes wrong ?

Mahmood_M
Posts: 20
Joined: Thu 24 Sep 2015 21:18

Re: Problem with SQL Server Computed Fields

Post by Mahmood_M » Sun 12 Nov 2017 13:28

Any suggestion ?

Stellar
Devart Team
Posts: 496
Joined: Tue 03 Oct 2017 11:00

Re: Problem with SQL Server Computed Fields

Post by Stellar » Mon 13 Nov 2017 13:20

The value of the calculated fields does not change when the record is refreshed, because they are not included in the record refresh query.
Try manually setting the refresh query for the record where there are Computed Columns:

UniTable.SQLRefresh.Text := ' SELECT FName, FFamily, MyField FROM Table WHERE KeyField = :KeyField ';

After changing the data in the database, you need to refresh the record in the dataset. You can refresh the record either manually TUniTable.RefreshRecord, or automatically by setting the RefreshOptions property of the TUniTable component when executing the refresh.
For example, refresh the record after inserting and updating:
UniTable.RefreshOptions: = [roAfterInsert, roAfterUpdate];

Refreshing the record after the change will solve the issue "Record was changed by another user".
Last edited by Stellar on Thu 16 Nov 2017 09:45, edited 2 times in total.

Mahmood_M
Posts: 20
Joined: Thu 24 Sep 2015 21:18

Re: Problem with SQL Server Computed Fields

Post by Mahmood_M » Tue 14 Nov 2017 14:15

Problem solved, thanks

Stellar
Devart Team
Posts: 496
Joined: Tue 03 Oct 2017 11:00

Re: Problem with SQL Server Computed Fields

Post by Stellar » Tue 14 Nov 2017 15:03

Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.

Post Reply