Board index » delphi » Simple Master - Detail Doesn't work

Simple Master - Detail Doesn't work

Hi! Folks,

I have a master-detail realtionship in the form of two TQuery objects,
both having CachedUpdates True, attached TUpdateSQL's and RequestLive =
False, the Datasource property of the detail query has been attached
properly.

The problem is while commiting the changes to the database, here is my
code.

try
  Database.StartTransaction;
  Query1.ApplyUpdates;
  Query2.ApplyUpdates;
  Database.Commit;
except
  ShowMessage(E.Message)
end;

On the execution of the Query1.Applyupdates statement I lose all the
changes made to the Query2. I broke my head on it for a long time and
tried something different like this

try
  Database.StartTransaction;
  Query2.DataSource := nil;.
  Query1.ApplyUpdates;
  Query2.Datasource := Datasource1;
  Query2.ApplyUpdates;
  Database.Commit;
except
  ShowMessage(E.Message)
end;

This does work but then it is really starnge why the first one doesn't
work, I tried by using a table at the detail level, and it worked, but
why not with a TQuery ???

TIA

Bala

 

Re:Simple Master - Detail Doesn't work


Bala,

I had the same problem and for weeks I banged my head against a wall.

The reason is that the Apply updates on the master closes the detail
query.

Andy.

Quote
xxxx wrote:

> Hi! Folks,

> I have a master-detail realtionship in the form of two TQuery objects,
> both having CachedUpdates True, attached TUpdateSQL's and RequestLive =
> False, the Datasource property of the detail query has been attached
> properly.

> The problem is while commiting the changes to the database, here is my
> code.

> try
>   Database.StartTransaction;
>   Query1.ApplyUpdates;
>   Query2.ApplyUpdates;
>   Database.Commit;
> except
>   ShowMessage(E.Message)
> end;

> On the execution of the Query1.Applyupdates statement I lose all the
> changes made to the Query2. I broke my head on it for a long time and
> tried something different like this

> try
>   Database.StartTransaction;
>   Query2.DataSource := nil;.
>   Query1.ApplyUpdates;
>   Query2.Datasource := Datasource1;
>   Query2.ApplyUpdates;
>   Database.Commit;
> except
>   ShowMessage(E.Message)
> end;

> This does work but then it is really starnge why the first one doesn't
> work, I tried by using a table at the detail level, and it worked, but
> why not with a TQuery ???

> TIA

> Bala

Re:Simple Master - Detail Doesn't work


Quote
Andrew Norman wrote:
> Bala,

> I had the same problem and for weeks I banged my head against a wall.

> The reason is that the Apply updates on the master closes the detail
> query.

> Andy.

Andy,

Were you able to solve the problem, if so then would you kindly let me
know the solution. Also why does the ApplyUpdates close the detail
relationship, if you know ?

Bala

Re:Simple Master - Detail Doesn't work


Quote
>I have a master-detail realtionship in the form of two TQuery objects,
>On the execution of the Query1.Applyupdates statement I lose all the
>changes made to the Query2.

This may seem a bit extreme, but...

We don't use Delphi's built-in master/detail properties in conjunction with
cached updates.  Even without the problem you're currently running into,
Delphi seems to "refresh" (close and open) the detail dataset a bit too
often, which can lead to poor performance and potential data loss.

You might consider writing a generic procedure that synchronizes masters and
details, and call that proc from within your DataSource's OnDataChange event
(when the variable Field = nil.  You should also make sure that you're not
inside of a posting routine, which we do by checking to see if there's an
active transaction.)

Our synchronization routine also checks the values of the link fields.  If
they have not changed, then we do nothing.  (This prevents the detail
dataset from being closed prematurely.)

-- Mark

Re:Simple Master - Detail Doesn't work


In article <35CAA0F0.F0F15...@giasbm01.vsnl.net.in>,
  Bala Iyer <da...@giasbm01.vsnl.net.in> wrote:

Quote
> Andrew Norman wrote:

> > Bala,

> > I had the same problem and for weeks I banged my head against a wall.

> > The reason is that the Apply updates on the master closes the detail
> > query.

> > Andy.

> Andy,

> Were you able to solve the problem, if so then would you kindly let me
> know the solution. Also why does the ApplyUpdates close the detail
> relationship, if you know ?

> Bala

I didn't read you original message, but I presume that you are using the
"datasource" property to link the queries.  Why does the ApplyUpdates close
the detail?  I wish I knew - it certainly diminishes the usefulness of the
feature.  In fact, anything that causes the Master to move its cursor closes
the Detail - this could be a "Next" (which makes sense), but also "Post", and
of course, "ApplyUpdates" (the latter two don't make sense to me). Basically,
it means that you should not use the "datasource" property to establish the
Master/Detail relationship - take care of the openning/closing of the
datasets yourself, and set the parameter values yourself. Cheers DZ

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Other Threads