Recent

Author Topic: get record numer from dataset  (Read 1197 times)

xaver

  • Newbie
  • Posts: 4
get record numer from dataset
« 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.




dsiders

  • Hero Member
  • *****
  • Posts: 515
Re: get record numer from dataset
« Reply #1 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.
Lazarus 2.0.12, 2.2.0-RC1, 2.3.0 (Git) / FPC 3.2.0, 3.2.2 / x86-win64 / Windows 8.1

egsuh

  • Hero Member
  • *****
  • Posts: 834
Re: get record numer from dataset
« Reply #2 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

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.

Zvoni

  • Hero Member
  • *****
  • Posts: 708
Re: get record numer from dataset
« Reply #3 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?
« Last Edit: May 03, 2021, 10:12:59 am by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

 

TinyPortal © 2005-2018