Saving and restoring rich text to/from database


I am creating a winform app in C# to store formatted text from a rich text box to MySQL database, which can be retrieved back to the rich text box. The database field is a VARCHAR and my code is something similar to below. But I’m getting “file not in correct format” error. Can anyone tell what could be the problem? Is the VARCHAR field okay to store it or should I change it to a BLOB?

string rtfText = this.richTextBox1.Rtf;
// save rtfText to database field as varchar
// …
// reload rtfText from database as string
this.richTextBox1.Rtf = rtfText;

utf-8 varchar to store 2 bytes per character similar to the encoding used by .Net strings.

Thanks for your reply. I changed the database field to VARCHAR CHAR SET utf-8, but still getting the same error.

When I compare the string values (“Test”) they are not the same.
strRtf = {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 test\par}

When I retrieve it from the database:
richTextBox1.Rtf = {tf1ansiansicpg1252deff0deflang1033{fonttbl{f0fnilfcharset0 Microsoft Sans Serif;}}viewkind4uc1pardf0fs17 testpar}

Did you saved it again before loading it?

You probably want to use binary storage here, I think that is called IMAGE in mysql.