Update joined query with TClientDataset

824 views Asked by At

I have a SQL Server DB with these tables:

CREATE TABLE [dbo].[Table_1](
    [key1] [nchar](50) NOT NULL,
    [key2] [nchar](50) NOT NULL,
    [data1] [nchar](10) NULL,
    [data2] [nchar](10) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Table_2](
    [key1] [nchar](50) NOT NULL,
    [key2] [nchar](50) NOT NULL,
    [data3] [nchar](10) NULL,
    [data4] [nchar](10) NULL
) ON [PRIMARY]

I have a query that pulls in all the fields:

select 
  t1.key1, t1.key2, t1.data1, t1.data2,
  t2.key1, t2.key2, t2.data3, t2.data4
from Table_1 t1 join Table_2 t2 on 
  t1.key1 = t2.key1 and t1.key2 = t2.key2

I have a form with a TDBGrid, a TADOConnection, a TADOQuery, a TDatasetProvider, and a TDataSource. I have everything connected so that the grid displays the expected data from the query in design mode. The grid is not readonly, and all of the assorted datasets have CanModify true.

The form has a menu with an item which has the following OnClick method

procedure TCdsToy0Form.ApplyUpdates1Click(Sender: TObject);
begin
  (dsCDStoGrid.DataSet as TClientDataSet).ApplyUpdates(0);
end;

When I use the grid to edit a value for a data1 column and then click my Apply Updates menu item, I get this message:

Exception class EOleException with message 'Invalid column name 'key1_1''. 

What am I doing wrong, or failing to do?

1

There are 1 answers

0
JohnS On

When a ClientDataset is based on joined tables it may not be able to determine which columns/table to update. In this case you need to tell it how. See this article which explains what you need to do. Basically you may have to implement code in OnGetTableName.