Jan Doggen
Jan Doggen

Reputation: 9106

Mismatch in datapacket error on TClientDataSet LoadFromStream

I get a 'Mismatch in datapacket' error on the lCDS2.LoadFromStream in the function below.

AFile contains a nested TClientDataSet written to disk using SaveToFile(filename), so dfBinary. The sTranslate (=TT_Translate) field of the first and only record is of type ftDataSet and contains the 'second' dataset.
The file is not corrupt.

    function TDataModuleBewerkLanguage.IsOldLanguageFormat(const AFile: String): Boolean;
    var
      lCDS1,
      lCDS2 : TClientDataSet;
      lTS   : TStringStream;
    begin
      lCDS1 := TClientdataSet.Create(nil);
      lCDS2 := TClientdataSet.Create(nil);
      try
         lCDS1.LoadFromFile(AFile);  
         if lCDS1.FieldByName(sTranslate).DataType <> ftDataSet then
            Raise Exception.Create('TT_TRANSLATE no dataset field');
         lTS := TStringStream.Create(lCDS1.FieldByName(sTranslate).AsString);
         try
            lTS.Position := 0;
            lCDS2.LoadFromStream(lTS);
            Result := (lCDS2.FieldCount <= 3);
         finally
            lTS.Free;
         end;
      finally
         lCDS1.Free;
         lCDS2.Free;
      end;
    end;

I tried TStringStream.Create without encoding, with TEncoding.Unicode, TEncoding.ANSI, TEncoding.ASCII, TEncoding.UTF8 Peeking into the file with a hex editor I get the impression it is not a character encoding issue:

enter image description here

How to solve the issue?
Maybe avoid the TStringStream altogether?

The two SO questions about the same error do not seem relevant.

This a 64 bit app in Delphi 12.

Upvotes: 0

Views: 91

Answers (1)

Jan Doggen
Jan Doggen

Reputation: 9106

I found a workaround thanks to this this answer. It does not explain why the LoadfromStream fails, but it retrieves the nested dataset another (easier and 'cleaner') way. Forget the streams, just do:

 lCDS2 := TClientDataSet(TDataSetField(lCDS1.FieldByName(sTranslate)).NestedDataSet);

And remove both the lCDS2 Create and Free of course ;-)

Upvotes: 0

Related Questions