Lazarus

Programming => Databases => Topic started by: xaver on May 01, 2021, 03:20:40 am

Title: get record numer from dataset
Post by: xaver on May 01, 2021, 03:20:40 am
Hi,

I'm desperately trying to get the entire record Nr. from a data set:

function TForm1.DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
  i: Integer;
begin
  aDataset.RecNo := aRecNo;
  Result := '';
  for i := 0 to aDataset.FieldCount - 1 do
    Result := Result + aDataset.Fields.AsString + ' ';

  Result := Trim(Result);
end;

procedure TForm1.Button9Click(Sender: TObject);
var i :Integer;
  row : String;
 
begin
     for i:=0 to DataSource1.DataSet.RecordCount -1 do
          row:= DataRowToString(DataSource1.DataSet, i);
          Memo1.Lines.Add(row);
end;

Unfortunately I'm receiving the following error message:

CSVDataset1 : Could not find the requested record.

Could someone help explaining with an example how to get a specific record by index ?

Thanks.

Xaver.



Title: Re: get record numer from dataset
Post by: dsiders on May 01, 2021, 04:12:08 am
Hi,

I'm desperately trying to get the entire record Nr. from a data set:

function TForm1.DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
  i: Integer;
begin
  aDataset.RecNo := aRecNo;
  Result := '';
  for i := 0 to aDataset.FieldCount - 1 do
    Result := Result + aDataset.Fields.AsString + ' ';

  Result := Trim(Result);
end;

procedure TForm1.Button9Click(Sender: TObject);
var i :Integer;
  row : String;
 
begin
     for i:=0 to DataSource1.DataSet.RecordCount -1 do
          row:= DataRowToString(DataSource1.DataSet, i);
          Memo1.Lines.Add(row);
end;

Unfortunately I'm receiving the following error message:

CSVDataset1 : Could not find the requested record.

Could someone help explaining with an example how to get a specific record by index ?

Thanks.

Xaver.

Your loop iterates from 0 to RecCount-1 and valid record numbers are in the range 1..RecCount.
Title: Re: get record numer from dataset
Post by: egsuh on May 01, 2021, 09:39:01 am
You should not try to use RecNo.  Please refer to the following page.

https://lazarus-ccr.sourceforge.io/docs/fcl/db/tdataset.recno.html (https://lazarus-ccr.sourceforge.io/docs/fcl/db/tdataset.recno.html)

It says, first, recno property must be implemented by TDataSet's descendants. Secondly, it says not to use it because it is not reliable.

BTW, dataset is a set, not ordered list. You'd better implement some other method to identify a specific record.
Title: Re: get record numer from dataset
Post by: Zvoni on May 03, 2021, 08:25:40 am
What the others said about RecNo not withstanding:
You seem to try to concatenate all Fields to a single string.
Why not read all fields into an array, and then send the array to a Join?
https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.join.html
Note: Any Fields, which are "empty" you have to handle yourself (a.k.a include/exclude from the result)

EDIT:
Quote
BTW, dataset is a set, not ordered list. You'd better implement some other method to identify a specific record.
Nevermind, that the source for it is probably a Query.
This bears the question: Why not concat it within the SQL-Statement?
Which brings us to the next question: Which DBMS?
TinyPortal © 2005-2018