Board index » delphi » help to use ClientDataSet, problem with ApplyUpdates

help to use ClientDataSet, problem with ApplyUpdates


2005-05-09 02:22:10 AM
delphi54
Hello, its my first topic, I am a brazilian, and I have a poor english..
sorry! :-)
But, it is my problem, I use Firebird 1.5 SuperServer with DBExpress:
I had a project, with ClientDataSet+SQLDataSet+ConnectionBroker... I use
MultiTier... I have this code:
with DM_Dados do
begin
cdsItens_VendaID_ITENS_VENDA.AsInteger := 0; <- These is my primary key (I
get the new key on BeforeInsert in my Database)
cdsItens_VendaID_VENDA.AsString := cdsVendaID_VENDA.AsString; <- these is
my key os Master-Detail (father)
if cdsItens_Venda.State = dsInsert then
begin
cdsItens_Venda.Post;
cdsItens_Venda.ApplyUpdates(0);
if Application.MessageBox('Deseja adicionar um novo produto?',
'SiacomFast', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1 + MB_APPLMODAL) =
ID_YES then
begin
cdsItens_Venda.Insert;
DBEdit1.SetFocus;
end
else
begin
cdsItens_Venda.Refresh; <------ Here is the error
Close;
end;
end;
end;
whenI try to Refresh my CDS, these error appear: cdsItens_Venda: Must apply
updates before refreshing data.
Why? I use POST and APPYUPDATE, why these is not work?
Thanks for all,
Fellipe H.
 
 

Re:help to use ClientDataSet, problem with ApplyUpdates

ApplyUpdates may not succeed and this way the change log is not cleared
and you are not allowed to call Refresh.
Handle OnReconcileError (use the standard error dialog as Delphi help
suggests) to see what error occurs.
 

Re:help to use ClientDataSet, problem with ApplyUpdates

Use Open and Close instead of refresh
"Fellipe Henrique" <XXXX@XXXXX.COM>writes
Quote
Hello, its my first topic, I am a brazilian, and I have a poor english..
sorry! :-)

But, it is my problem, I use Firebird 1.5 SuperServer with DBExpress:


I had a project, with ClientDataSet+SQLDataSet+ConnectionBroker... I use
MultiTier... I have this code:
with DM_Dados do
begin
cdsItens_VendaID_ITENS_VENDA.AsInteger := 0; <- These is my primary key
(I
get the new key on BeforeInsert in my Database)

cdsItens_VendaID_VENDA.AsString := cdsVendaID_VENDA.AsString; <- these is
my key os Master-Detail (father)


if cdsItens_Venda.State = dsInsert then
begin
cdsItens_Venda.Post;
cdsItens_Venda.ApplyUpdates(0);
if Application.MessageBox('Deseja adicionar um novo produto?',
'SiacomFast', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1 + MB_APPLMODAL) =
ID_YES then
begin
cdsItens_Venda.Insert;
DBEdit1.SetFocus;
end
else
begin
cdsItens_Venda.Refresh; <------ Here is the error
Close;
end;
end;
end;

whenI try to Refresh my CDS, these error appear: cdsItens_Venda: Must
apply
updates before refreshing data.

Why? I use POST and APPYUPDATE, why these is not work?

Thanks for all,

Fellipe H.


 

Re:help to use ClientDataSet, problem with ApplyUpdates

Hello, I use Close, Open instead of refresh, but the error is changed:
cdsItens_Venda: Dataset not in edit or insert mode.
my new code is here:
with DM_Dados do
begin
cdsItens_VendaID_ITENS_VENDA.AsInteger := 0;
cdsItens_VendaID_VENDA.AsString := cdsVendaID_VENDA.AsString;
if cdsItens_Venda.State = dsInsert then
begin
cdsItens_Venda.Post;
cdsItens_Venda.ApplyUpdates(0);
if Application.MessageBox('Deseja adicionar um novo produto?',
'SiacomFast', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1 + MB_APPLMODAL) =
ID_YES then
begin
cdsItens_Venda.Insert;
DBEdit1.SetFocus;
end
else
begin
cdsItens_Venda.Close;
cdsItens_Venda.Open;
cdsItens_Venda.Refresh;
Close;
end;
end;
end;
where is the error now?
thanks for all
Fellipe H.
"Mike Jacobson" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
Use Open and Close instead of refresh


"Fellipe Henrique" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...
>Hello, its my first topic, I am a brazilian, and I have a poor english..
>sorry! :-)
>
>But, it is my problem, I use Firebird 1.5 SuperServer with DBExpress:
>
>
>I had a project, with ClientDataSet+SQLDataSet+ConnectionBroker... I use
>MultiTier... I have this code:
>with DM_Dados do
>begin
>cdsItens_VendaID_ITENS_VENDA.AsInteger := 0; <- These is my primary key
>(I
>get the new key on BeforeInsert in my Database)
>
>cdsItens_VendaID_VENDA.AsString := cdsVendaID_VENDA.AsString; <- these
is
>my key os Master-Detail (father)
>
>
>if cdsItens_Venda.State = dsInsert then
>begin
>cdsItens_Venda.Post;
>cdsItens_Venda.ApplyUpdates(0);
>if Application.MessageBox('Deseja adicionar um novo produto?',
>'SiacomFast', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1 + MB_APPLMODAL)
=
>ID_YES then
>begin
>cdsItens_Venda.Insert;
>DBEdit1.SetFocus;
>end
>else
>begin
>cdsItens_Venda.Refresh; <------ Here is the error
>Close;
>end;
>end;
>end;
>
>whenI try to Refresh my CDS, these error appear: cdsItens_Venda: Must
>apply
>updates before refreshing data.
>
>Why? I use POST and APPYUPDATE, why these is not work?
>
>Thanks for all,
>
>Fellipe H.
>
>


 

Re:help to use ClientDataSet, problem with ApplyUpdates

1) You don't need to call Refresh, after Close- Open
2) Close- Open is not equivalent to Refresh. If Cds.FileName is defined
then Close- Open will not refetch the data from server. If
Cds.FileName='' then Close-Open will fetch data from server, but it will
not keep the current record (Refresh tries to do that instead)
3) Use code like the following:
i:=Cds.ApplyUpdates(0);
If i<>0 then
ShowMessage(Format('%d errors found',[i]))
else
Cds.Refresh;
or handle OnReconcileError as I have already suggested
 

Re:help to use ClientDataSet, problem with ApplyUpdates

Hello, I put the ReconcileError Dialog, and the message is:
violation of FOREIGN KEY constraint "INTEG_403" on table "ITENS_VENDA"
violation of FOREIGN KEY constraint ""
Error Code: 146
I now, these error is because my child table is not save the key of my
master table, but look:
I put in the MasterSource properties of my detail table, to cdsVendas, and
my MasterFields ID_Venda ->ID_Venda, when I open my detail table in a
DBGrid, the key os master table appears in the column, but when I
ApplyUpdate, these error appears...
I try to put these code
cdsItens_VendaID_VENDA.AsString := cdsVendaID_Venda.AsString;
on OnNewRecord event, and the error continue....
How I use master detail link? I need to finish these, my boss is kill
me..:-)
Thanks for All...
Fellipe H.
"Kostas Terzides" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
1) You don't need to call Refresh, after Close- Open

2) Close- Open is not equivalent to Refresh. If Cds.FileName is defined
then Close- Open will not refetch the data from server. If
Cds.FileName='' then Close-Open will fetch data from server, but it will
not keep the current record (Refresh tries to do that instead)

3) Use code like the following:

i:=Cds.ApplyUpdates(0);
If i<>0 then
ShowMessage(Format('%d errors found',[i]))
else
Cds.Refresh;

or handle OnReconcileError as I have already suggested
 

Re:help to use ClientDataSet, problem with ApplyUpdates

If I understand correct you have formed the master- detail relationship
in client side (using Cds.mastersource property) and you try to insert a
master record and then a detail record. You could do the following:
CdsMaster.ApplyUpdates(0);
CdsMaster.Refresh; //This has to be done to get the permanent ID values
CdsDetail.Refresh; //This also if you have any cascades in database
//Here insert the new detail record as you do
CdsDetail.ApplyUpdates(0);
CdsDetail.Refresh;
This of course, will cause huge network traffic. The recommended
approach is to build the master- detail relationship in application
server (that is use nested datasets) and read this article (and adjust
to your application requirements):
bdn.borland.com/article/0,1410,20847,00.html
 

Re:help to use ClientDataSet, problem with ApplyUpdates

Hello,
The error persist... :-(
I see something, when I type the record in IBExpert, the same error appears,
I have one record in my Master CDS, and the error appear...
for these, I now the error is not in my project, it is in my database...
here is, my DDL, of my Detail table:
CREATE TABLE ITENS_VENDA (
ID_ITENS_VENDA INTEGER NOT NULL,
ID_VENDA INTEGER NOT NULL,
ID_SUFIXO INTEGER,
CODIGO_MESTRE VARCHAR(20),
DESCRICAO VARCHAR(50),
ID_MARCAS INTEGER,
QUANTIDADE INTEGER,
VALOR_UNITARIO DECIMAL(15,2),
DESCONTO DECIMAL(15,2),
PRECO_LIQUIDO DECIMAL(15,2),
VALOR_TOTAL DECIMAL(15,2),
LUCRO DECIMAL(15,2),
VALOR_PADRAO DECIMAL(15,2),
ID_PRODUTO INTEGER
);
ALTER TABLE ITENS_VENDA ADD PRIMARY KEY (ID_ITENS_VENDA);
ALTER TABLE ITENS_VENDA ADD FOREIGN KEY (ID_PRODUTO) REFERENCES C_PRODUTOS
(ID_PRODUTO);
ALTER TABLE ITENS_VENDA ADD FOREIGN KEY (ID_VENDA) REFERENCES VENDA
(ID_VENDA) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE INDEX XIF1ITENS_VENDA ON ITENS_VENDA (ID_VENDA);
CREATE INDEX XIF2ITENS_VENDA ON ITENS_VENDA (ID_PRODUTO);
CREATE UNIQUE INDEX XPKITENS_VENDA ON ITENS_VENDA (ID_ITENS_VENDA,
ID_VENDA);
thanks for all
Fellipe H.
"Kostas Terzides" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
If I understand correct you have formed the master- detail relationship
in client side (using Cds.mastersource property) and you try to insert a
master record and then a detail record. You could do the following:

CdsMaster.ApplyUpdates(0);
CdsMaster.Refresh; //This has to be done to get the permanent ID values
CdsDetail.Refresh; //This also if you have any cascades in database
//Here insert the new detail record as you do
CdsDetail.ApplyUpdates(0);
CdsDetail.Refresh;

This of course, will cause huge network traffic. The recommended
approach is to build the master- detail relationship in application
server (that is use nested datasets) and read this article (and adjust
to your application requirements):
bdn.borland.com/article/0,1410,20847,00.html


 

Re:help to use ClientDataSet, problem with ApplyUpdates

"INTEG_403" foreign key error is either because ID_VENDA field or
because ID_PRODUTO field of ITENS_VENDA table doesn't have a valid value.
You should also (if not already doing so) set a valid value in
OnNewRecord for ID_PRODUTO field
 

Re:help to use ClientDataSet, problem with ApplyUpdates

Yes, I put the valid value for ID_PRODUTO, and I use IBExpert, don't use my
project, and in IBExpert the error appears... I look, the values of FK, is
valid...
what type os error is it?
thanks
Fellipe H.
"Kostas Terzides" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
"INTEG_403" foreign key error is either because ID_VENDA field or
because ID_PRODUTO field of ITENS_VENDA table doesn't have a valid value.
You should also (if not already doing so) set a valid value in
OnNewRecord for ID_PRODUTO field
 

Re:help to use ClientDataSet, problem with ApplyUpdates

I don't know , may be someone more experienced in these ng can help you.
 

Re:help to use ClientDataSet, problem with ApplyUpdates

Thanks for all Kostas,
I dont now how I fix these, but for this time, I remove the FK in my
database, and my project works fine... when a get more time, I try to do
these...
Thanks for all Kosta, and thanks for your patience,
[]s
Fellipe H.
"Kostas Terzides" <XXXX@XXXXX.COM>escreveu na mensagem
Quote
I don't know , may be someone more experienced in these ng can help you.