Good day to all. They say that life without problems is boring, but I would like to live without problems for at least a year. I work with the xml file, and I need to delete all the entries. My code is:

if dm.ClientDataSet2.Active=false then dm.ClientDataSet2.Open; dm.ClientDataSet2.Edit; dm.ClientDataSet2.First; while not(dm.ClientDataSet2.RecordCount=0) do dm.ClientDataSet2.delete; dm.ClientDataSet2.SaveToFile(); dm.ClientDataSet2.MergeChangeLog; 

When the button is pressed for the first time, nothing happens; when it is pressed for the second time, some records remain in the file, only from the third time the file is completely cleared. Tell me, what is wrong here? In the code, it seems, everything is fine.

  • Format the code using the 101010 button, otherwise reading is not possible - SoftR

2 answers 2

And if you try

 while not dm.ClientDataSet2.IsEmpty do dm.ClientDataSet2.delete; 

And Next in the cycle does not need to be set, because after deleting a record, the cursor moves to the next record, and if you also insert Next after deleting, it turns out that every second record will be deleted. Also try deleting this line here.

 dm.ClientDataSet2.Edit; 

When deleting, you do not need to get into edit mode, just open the data.

  • Thank you very much, everything turned out. - prapar

here you go

  dm.ClientDataSet2.First; 

I see where you are

 dm.ClientDataSet2.Next; 

Yes, and instead

 while not(dm.ClientDataSet2.RecordCount=0) do 

Probably better to use.

 while not(dm.ClientDataSet2.EOF) do 
  • Followed your advice, the result is the same. In the file I have only 270 entries and 6 columns. - prapar
  • In your piece of code, nothing really is not clear. What I advised was what immediately caught my eye, and the very formulation of the problem is very blurry. Formulate all the normal can be tedious chewed language. - SoftR
  • Thanks, I figured it out - prapar