Dev
Dev

Reputation: 639

TParam.LoadFromStream is not working in Delphi XE2?

I have written a below code in Delphi XE2.

var
stream : TStringStream;

begin
stream := TStringStream.Create;

//Some logic to populate stream from memo.

ShowMessage(stream.datastring); //This line is showing correct data

// some Insert query with below parameter setting
ParamByName('Text').LoadFromStream(stream , ftMemo);

But this is storing text as ???? in table.

This type of code is working fine in Delphi 4.

Is there any issue in TParam.LoadFromStream function in Delphi XE2?

EDIT: Table field is of type 'Text'.

Upvotes: 1

Views: 2149

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596256

The root of your issue is that TStringStream does not operate the same way in D2009+ as it did in D4.

In D4, TStringStream was a simple wrapper around an AnsiString variable. The DataString property simply returned a direct reference to that variable, and all reads/writes operated directly on the contents of the variable. The stream's bytes and String characters were basically one and the same thing back then.

In D2009+, TStringStream is now a wrapper around a TBytes array of encoded bytes instead, where the default encoding is the default Ansi encoding of the OS that your app is running on. If you write a string to the stream using WriteString(), it gets encoded from Unicode to bytes using the stream's encoding, and then those encoded bytes are stored. If you read a string from the stream using ReadString(), or read the DataString property, the stored bytes are decoded to a Unicode string. Any other read/write operations operate on the raw encoded bytes instead, like any other stream type would. So when you call TParam.LoadFromStream(), it is reading the raw encoded bytes, not a Unicode string. The stream's raw bytes and String characters are NOT one and the same thing anymore. So the data you see in the ShowMessage() is not the same data that TParam sees.

Upvotes: 1

Dev
Dev

Reputation: 639

After doing some trial and error methods I have found a solution to this problem. We can use below code,

ParamByName('Text').AsMemo := SampleMemo.Text;

Upvotes: 1

Related Questions