-5

Following code works fine but don't know why it omits the first record. However, if remove this line ListViewMain.Items.Clear; on the second call it displays correctly all records. i am using SQLite.

procedure TFMain.BtnUpCommingClick(Sender: TObject);
var t: string   ;
ListItem: TListViewItem;
FullName: TField;
SimNumber: TField;
begin
  ChangeTabAction2.ExecuteTarget(Self);

  t := DateToStr(Tomorrow);

  FDQueryUpComming.SQL.Text:='Select * from TCustomer Where PackageDate = :PackageDate';
  FDQueryUpComming.ParamByName('PackageDate').AsString := t;
  FDQueryUpComming.Open;
  if FDQueryUpComming.RecordCount = 0 then Exit;

  FullName:= FDQueryUpComming.FieldByName('CName');
  SimNumber:= FDQueryUpComming.FieldByName('CSim');
  FDQueryUpComming.First;   // move to the first record
  ListViewMain.Items.Clear;
  ListViewMain.BeginUpdate;
  try
    while(not FDQueryUpComming.Eof)do begin
      ListItem:= ListViewMain.Items.Add;
      ListItem.Text:= FullName.AsString;
      ListItem.Detail:= SimNumber.AsString;
      FDQueryUpComming.Next;
    end;
  finally
    ListViewMain.EndUpdate;
  end;
end;
Fiaz
  • 30
  • 2
  • 9
  • It shouldn not happen. If it does, there is something wrong. Actually, calling `First` is not necessary here because you're not moving cursor lines before and after opening dataset you're with the cursor on the first record. Just one last note, if that is a single purpose query object, better open it once and then only modify parameter(s) and call `Refresh`. – Victoria Aug 27 '17 at 15:31
  • yes it is really strange i try to add more items but still skipping first items only all other is fine. – Fiaz Aug 27 '17 at 16:10
  • Usage of parameter in your case is suspicious (what is the data type of `PackageDate`? Have you created your table using FireDAC's pseudo data types for SQLite?). Are you sure this event method doesn't simply exit with 0 record count and you filled your list view in some previously called method? – Victoria Aug 27 '17 at 16:12
  • the datatype of Pacakgedate is text. No it doesn't exit with 0 and neither calling any previously called method. – Fiaz Aug 27 '17 at 16:42
  • Well, then I can only suggest to file a bug report. What you're doing seems to be correct. – Victoria Aug 27 '17 at 16:51
  • What does `ChangeTabAction2.ExecuteTarget(Self)` do? – MartynA Aug 27 '17 at 17:48
  • @MartynA, ..and the rest of the code we cannot see :) From your comment it sounds you cannot find any problem in the presented code (like me). – Victoria Aug 27 '17 at 18:59
  • 2
    @Victoria: Yes, I completely agree with you. Why the OP couldn't post an MCVE in the first place instead of leaving readers to guess, I don't know. I'm voting to close this Q until he does. – MartynA Aug 27 '17 at 19:01
  • Do you experience the same issue when using `ListViewMain.Items.BeginUpdate / EndUpdate`? When debugging is the number of records in your dataset as expected? – nil Aug 27 '17 at 20:04
  • @MartynA ChangeTabAction2.ExecuteTarget(Self) is calling my second tab on the main page. i have 2 tab on my main form. Mian tab i have a button which on click will take user to the second tab and add the desired result into listview. – Fiaz Aug 28 '17 at 06:19
  • @Nil ListViewMain.items.BeginUpdate is not available only availbale ListviewMain.BeginUpdate. – Fiaz Aug 28 '17 at 06:20
  • 2
    You should delete this q - it has zero value for future readers. – MartynA Aug 28 '17 at 09:41
  • @MartynA can't able to delete can you please. – Fiaz Aug 28 '17 at 09:46
  • I already delete my answer now please help me to delete my question next time i will be extra careful before ask. However, i would say i am true to my fellow members once i found my mistake i posted here not even think what you will think and how you will treat. – Fiaz Aug 28 '17 at 09:51
  • 1
    @Fiaz Thank you for your concern and for trying to rectify the mistake. [Look at this help page](https://stackoverflow.com/help/deleted-questions) for some hints why it´s not so easy to delete questions. Even though you deleted your own answer the only remaining one has one upvote (plus one downvote, therefore sum = 0) and that prevents your deletion of the question. I think you will have to leave the rest to the moderators, but check back here from time to time just in case. – Tom Brunberg Aug 28 '17 at 11:53
  • Out of curiousity, what was the answer, the solution? – nil Aug 28 '17 at 13:25
  • 1
    @Nil The reason for the question was a mistake OP had done. So there is no question, no answer and no solution. – Tom Brunberg Aug 28 '17 at 15:04

1 Answers1

-2

it seems to have no error in your code, but is the problem in the add to listview or in the table ?

did you have the same problem testing on Windows and on Android or only for Android ?

please try this code :

procedure TFMain.BtnUpCommingClick(Sender: TObject);
var t: string   ;
ListItem: TListViewItem;
FullName: TField;
SimNumber: TField;
begin
  ChangeTabAction2.ExecuteTarget(Self);

  t := DateToStr(Tomorrow);

  FDQueryUpComming.SQL.Text:='Select * from TCustomer Where PackageDate = :PackageDate';
  FDQueryUpComming.ParamByName('PackageDate').AsString := t;
  FDQueryUpComming.Open;
  if FDQueryUpComming.RecordCount = 0 then Exit;

  ListViewMain.BeginUpdate;
  ListViewMain.Items.Clear;
  try
    FDQueryUpComming.First;   // move to the first record
    repeat
      ListItem:= ListViewMain.Items.Add;
      ListItem.Text:= FDQueryUpComming.FieldByName('CName').AsString;
      ListItem.Detail:= FDQueryUpComming.FieldByName('CSim').AsString;
      FDQueryUpComming.Next;
    until FDQueryUpComming.Eof;
  finally
    ListViewMain.EndUpdate;
  end;
end;

add a breakpoint on the first line after "repeat" and check if the problem come from the database / Firedac or the ListView component.

  • 2
    You shouldn't use a `repeat` loop to traverse a dataset which might be empty. And the OP's code is better because it does not use `FieldByName` in the loop. – MartynA Aug 27 '17 at 17:50
  • Agree with you but for testing it's easier. And he tests the number of records before the loop, so the repeat...until can work in this case. – Patrick PREMARTIN Aug 27 '17 at 18:27
  • 1
    Why is it easier/better to use worse code (`repeat` and `FieldByName') when the debugger can perfectly well be used to investigate what is going on with the OP's code? – MartynA Aug 27 '17 at 19:42
  • Simply because simplifying/changing the code can help to find where the bug is and eliminate potential Delphi bugs. If the debugger is helpfull enough in this case, why do we have this discussion ? – Patrick PREMARTIN Aug 28 '17 at 08:54