Question on saving to DB Field

General TRichView support forum. Please post your questions here
Post Reply
rv777
Posts: 61
Joined: Wed Dec 08, 2010 3:32 pm

Question on saving to DB Field

Post by rv777 »

I'm a new user of TRichView and I have now a problem, as I don't konw what is wrong.

I have a TDBRichView connected to a DB Field and an external TRichViewEdit to edit the content of that field. The TDBRichView is only used to display the content of the DB field.

I pass the db field content like this to my editor:

Code: Select all

// MyTableRVSource is a blob field 
frmHTMLEditor.RVSource := MyTableRVSource.AsString;

This is the RVSource property set procedure:
procedure TfrmHTMLEditor.SetRVSource(const Value: String);
var
  Stream: TStringStream;
begin
  if Value <> '' then
  begin
    Stream := TStringStream.Create(Value);
    try
      Stream.Position := 0;
      RichViewEdit.LoadRVFFromStream(Stream);
      RichViewEdit.Format;
    finally
      Stream.Free;
    end;
  end;
end;
For saving the edited content of the TRichViewEdit back to the DB Field I use this code:

Code: Select all

function TfrmHTMLEditor.GetRVSource: String;
var
  Stream: TStringStream;
begin
(* This works, but I don't want do create a file!
   RichViewEdit.SaveRVF('d:\test.rvf', False);
  Stream := TFileStream.Create('d:\test.rvf', fmOpenRead or fmShareDenyNone);
  try
     SetLength(Result, Stream.Size);
     Stream.ReadBuffer(Pointer(Result)^, Stream.Size);
  finally
    Stream.Free;
  end;    
*)

    Stream := TStringStream.Create('');
    try
      RichViewEdit.SaveRVFToStream(Stream, False);
      Stream.Position := 0;
      Result := Stream.DataString;
    finally
      Stream.Free;
    end;
end;
On the calling form I do this then:
MyTableRVSource.AsString := frmHTMLEditor.RVSource;
MyTable.Post;


Now the problem ist, that this doesnt work and the resulting db Field content isn't the same as if I save the content of the RichViewEdit to an external File. The DBRichView diaplays only unreadable chars and no RichView formated text.

it looks like this in TDBRichView:

Code: Select all

-8 1 3
-7 0 -1 0 0 0 0 536870911
-9 2 0 0 2 0 1
RVStyle
 
........

Saving the content to an external file with TRichViewEdit.SaveRVF results in a file size of 79'065 bytes, while the content of SaveRVFToStream results only in 69'705 bytes. Why?

Any idea what I'm doing wrong? [/code]
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Version of Delphi?
rv777
Posts: 61
Joined: Wed Dec 08, 2010 3:32 pm

Post by rv777 »

Ah sorry, Delphi 7 without any unicode components. Pure Delphi 7 ANSI.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Hmm. I am not sure, probably TStringStream does something to line breaks.

Try memory stream instead:

Code: Select all

procedure TfrmHTMLEditor.SetRVSource(const Value: String); 
var 
  Stream: TMemoryStream; 
begin 
  RichViewEdit.Clear;
  if Value <> '' then 
  begin 
    Stream := TMemoryStream.Create; 
    try 
      Stream.WriteBuffer(PChar(Value)^, Length(Value));
      Stream.Position := 0; 
      RichViewEdit.LoadRVFFromStream(Stream); 
     finally 
      Stream.Free; 
    end; 
  end; 
  RichViewEdit.Format; 
end;

function TfrmHTMLEditor.GetRVSource: String; 
var 
  Stream: TMemoryStream; 
begin 
    Stream := TMemoryStream.Create; 
    try 
      RichViewEdit.SaveRVFToStream(Stream, False); 
      Stream.Position := 0; 
      SetLength(Result, Stream.Size);
      Stream.ReadBuffer(PChar(Result)^, Length(Result));
    finally 
      Stream.Free; 
    end; 
end;
rv777
Posts: 61
Joined: Wed Dec 08, 2010 3:32 pm

Post by rv777 »

With TMemoryStream it works. It's not clear to me why, but there must be an difference. If I look at the TStringStream Source, I can't realy see the reason for this, as there isn't anything magical done with CR/LF.

Anyway thanks for your help.
Post Reply