TDataSet.Post error in 9.4.12, but not in 8.6.11

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
embronne
Posts: 3
Joined: Mon 22 Sep 2014 11:40

TDataSet.Post error in 9.4.12, but not in 8.6.11

Post by embronne » Tue 14 Oct 2014 09:57

I use the code below to copy tables from one database to another. It works fine when I use ODAC 8.6.11, but I get an error in 9.4.12.

More specifically: when I first copy a table with e.g. 4 columns, then one with 3 columns, I get an error on tblTarget.Post that a field from the first table can't be found.

The code:

Code: Select all

procedure TfrmMain.CopyTabel(const TableName: String);
var
  tblSource, tblTarget: TDataSet;
  i: Integer;
  ifield: Integer;
  fieldname: string;
  fldTarget: TField;
  fldSource: TField;
begin
  tblSource := sfmDCBron.GetTable(TableName); // see below
  if Assigned(tblSource) then
  begin
    try
      tblTarget := sfmDCDoel.GetTable(TableName);
      if Assigned(tblTarget) then
      begin
        try
          tblSource.First;
          for i := 0 to tblSource.RecordCount - 1 do
          begin
            tblTarget.Append;
            for ifield := 0 to tblSource.FieldCount - 1 do
            begin
              fieldname := tblSource.Fields[ifield].fieldname;
              fldSource := tblSource.Fields[ifield];
              if not fldSource.IsNull then
              begin
                fldTarget := nil;
                try
                  fldTarget := tblTarget.FieldByName(fieldname);
                except
                  if LowerCase(fieldname) <> 'rowid' then
                    raise;
                end;
                if fldTarget <> nil then
                begin
                  if (fldTarget.DataType = ftDate) OR (fldTarget.DataType = ftDateTime) OR (fldTarget.DataType = ftTime) then
                    fldTarget.AsDateTime := fldSource.AsDateTime
                  else
                    fldTarget.AsString := fldSource.AsString;
                end;
              end;
            end;
            tblTarget.Post;
            tblSource.Next;
          end;
        finally
          tblTarget.Close;
        end;
      end;
    finally
      tblSource.Close;
    end;
  end;
end;
Where GetTable does something like this:

Code: Select all

  try
    oratable.TableName := TableName; // of type TOraTable
    oratable.Open;
    Result := oratable;
  except
    Result := nil;
  end;
When I try this on the following database:

Code: Select all

create table a ( a integer not null, b integer, c integer );
alter table a add constraint pk_a primary key ( a );

create table x ( x integer not null, y integer );
alter table x add constraint pk_x primary key ( x );

insert into a ( a, b, c ) values ( 1, 2, 3 );
insert into a ( a, b, c ) values ( 2, 3, 4 );
insert into a ( a, b, c ) values ( 3, 4, 5 );

insert into x ( x, y ) values ( 1, 2 );
and first copy table a, then x, I get an error on the post for the (first) record in x: 'Field A not found'.

Is this expected? That seems unlikely since it used to work fine. Any suggestions?

Thanks, regards, Miel.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: TDataSet.Post error in 9.4.12, but not in 8.6.11

Post by AlexP » Tue 14 Oct 2014 12:24

Hello,

In your sample, the error will occur on an attempt to access the "A" field in the "X" table

fldTarget := tblTarget.FieldByName(fieldname);

And since there is no actually such field in the table, you will get the error 'Field 'A'' not found'

embronne
Posts: 3
Joined: Mon 22 Sep 2014 11:40

Re: TDataSet.Post error in 9.4.12, but not in 8.6.11

Post by embronne » Tue 14 Oct 2014 12:45

No, I don't get the error there.

Var fieldName in fldTarget := tblTarget.FieldByName(fieldname); never has value "A" for table "X".

The error doesn't occur until line tblTarget.Post; is reached for the first time for table "X", after table "A" was processed correctly.

Why would that be different between 9.4.12 and 8.6.11 anyway?

Thanks, regards, Miel.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: TDataSet.Post error in 9.4.12, but not in 8.6.11

Post by AlexP » Wed 15 Oct 2014 06:08

We can't reproduce the problem. Please send a complete sample demonstrating the problem to alexp*devart*com.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: TDataSet.Post error in 9.4.12, but not in 8.6.11

Post by AlexP » Fri 17 Oct 2014 08:03

Thank you for the sample, we have fixed the problem. Te fix will be included into the next ODAC version. Currently, to solve the problem, you should explicitly specify key fields for Oracle tables.

Post Reply