Recent

Author Topic: Adding field definition on (Buf)Dataset raises error  (Read 3984 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 538
Adding field definition on (Buf)Dataset raises error
« on: December 11, 2014, 02:15:28 am »
Hi All,
Please advise if it would be possible to add a field definition later on to a (buf)Dataset.
I am getting the attached error when trying to insert a record with the updated structure.

Code: [Select]
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, memds, db, BufDataset, FileUtil, Forms, Controls, Graphics,
  Dialogs, DBGrids, StdCtrls, test_dataset;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    BufferDST  : TBufDataset;
    DataSource2: TDataSource;
    DBGrid2: TDBGrid;
    Edit2: TEdit;
    Edit3: TEdit;
    MemDST     : TMemDataset;
    MemDST2     : TBufDataset;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);

var
  aTest : test;

begin

  BufferDST.First;
  BufferDST.Active:= true;

  DataSource1.DataSet:= BufferDST;
  DataSource1.Enabled:= true;

  MemDST.First;
  MemDST.Active:= true;

  DataSource2.DataSet:= MemDST2;
  DataSource2.Enabled:= true;

  aTest := Test.Create;
  Edit1.Text:= IntToStr(aTest.HowMany(BufferDST));
  aTest.Free;

  aTest := Test.Create;
  Edit2.Text:= IntToStr(aTest.HowMem(MemDST2));
  aTest.Free;

  //Edit3.Text:= IntToStr(MemDST.RecordCount);
end;

procedure TForm1.FormCreate(Sender: TObject);

var
  i : integer;
  vFieldDef:TFieldDef;

begin

  BufferDST := TBufDataset.Create(nil);
  BufferDST.ClearFields;
  BufferDST.FieldDefs.Add('ID',ftInteger);
  BufferDST.FieldDefs.Add('AccountCode',ftInteger);
  BufferDST.CreateDataset;

  BufferDST.Active:= true;
  BufferDST.Open;
  BufferDST.ReadOnly:= false;
  BufferDST.Edit;
  BufferDST.AppendRecord([1, 319229]);
  BufferDST.AppendRecord([2, 456789]);
  BufferDST.AppendRecord([3, 456789]);

  //MemDST    := TMemDataset.Create(nil);
  //MemDST.ClearFields;
  //MemDST.FieldDefs.Add('ID',ftInteger);
  //MemDST.FieldDefs.Add('AccountCode',ftInteger);
  //MemDST.Open;

  MemDST2  := TBufDataset.Create(nil);

      for I:=0 to BufferDST.FieldDefs.Count-1 do begin
      vFieldDef:=MemDST2.FieldDefs.AddFieldDef;
      with BufferDST.FieldDefs[I] do begin
        vFieldDef.Name:=Name;
        vFieldDef.DataType:=DataType;
        vFieldDef.Size:=Size;
        vFieldDef.Required:=Required;
      end;
    end;
      MemDST2.CreateDataset;
      //MemDST2.Close;
      MemDST2.FieldDefs.Add('a',ftInteger);
      //MemDST2.FieldDefs.Update;

  //BufferDST.First;
  //MemDST.CopyFromDataset(BufferDST);
  MemDST2.Open;
  MemDST2.Edit;
  MemDST2.AppendRecord([1, 319229, 1]);   //error is thrown
end;

end.

Lazarus 1.2.6 32 bit with FPC 2.6.4, Win 7 64 bit

Thank you
Lazarus 2.0.2 64b on Debian LXDE 10

LacaK

  • Hero Member
  • *****
  • Posts: 600
Re: Adding field definition on (Buf)Dataset raises error
« Reply #1 on: December 11, 2014, 07:08:59 am »
It seems, that MemDST2 contains only 2 fields (copied from BufferDST) and you are trying insert record with 3 fields.

May be, that you must exchange lines:
  MemDST2.CreateDataset;
  MemDST2.FieldDefs.Add('a',ftInteger);
To:
  MemDST2.FieldDefs.Add('a',ftInteger);
  MemDST2.CreateDataset;

tudi_x

  • Hero Member
  • *****
  • Posts: 538
Re: Adding field definition on (Buf)Dataset raises error
« Reply #2 on: December 11, 2014, 11:55:58 am »
The addition of field 'a' is done later on based on a condition only in my original program.
The scenario I think is like below:

a. the dataset is created (but not populated)
b. is later on possible to alter its field definition (without clear it)?

from documentation: http://lazarus-ccr.sourceforge.net/docs/fcl/db/tfielddefs.update.html
so I would think an update is possible...
Update notifies the dataset that the field definitions are updated, if it was not yet notified.


« Last Edit: December 11, 2014, 12:01:34 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

LacaK

  • Hero Member
  • *****
  • Posts: 600
Re: Adding field definition on (Buf)Dataset raises error
« Reply #3 on: December 11, 2014, 01:34:58 pm »
IMO once dataset is created (CreateDataset is called) there can not be FieldDefs updated in sense, that change of them affect also dataset fields.

tudi_x

  • Hero Member
  • *****
  • Posts: 538
Re: Adding field definition on (Buf)Dataset raises error
« Reply #4 on: December 13, 2014, 01:39:06 pm »
I understand - I was correlating the addition of new fields with the update option.

When would the update option be used (for ex. MemDST2.FieldDefs.Update)?

 
Lazarus 2.0.2 64b on Debian LXDE 10

LacaK

  • Hero Member
  • *****
  • Posts: 600
Re: Adding field definition on (Buf)Dataset raises error
« Reply #5 on: December 13, 2014, 07:06:09 pm »
IMO TFieldDefs.Update does update of FieldDefs in related DataSet using Field definition from underlaying physical dataset.
See: http://docwiki.embarcadero.com/Libraries/XE7/en/Data.DB.TFieldDefs.Update

So reverse operation as you are expecting. You are expecting, that DataSet is updated using current FieldDefs, but it does opposite: updates FieldDefs using field definition from dataset.

 

TinyPortal © 2005-2018