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;

6 Years
Discussion Span
Last Post by Mitja Bonca

It's possible that some of the formatting codes are confusing MySql. You don't say where you get the error though.

Have you looked at what richTextBox1.Rft contains and compared it to what rtfText contains?


Thanks for your reply. I am getting the error in the last line of code (this.richTextBox1.Rtf = rtfText).

I compared the strings ("test") and 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}

Also tried changing database field to VARCHAR CHARACTER SET utf-8, but did not make any difference.


From what you've posted it appears that the \ is being stripped out of the text. Try changing this string rtfText = this.richTextBox1.Rtf; to this string rtfText = this.richTextBox1.Rtf.Replace(@"\", @"\\");

Edited by Momerath: n/a


I tried

string rtfText = this.richTextBox1.Rtf.Replace(@"\", @"\\")

and it worked!

When I try to save longer string though, I get database error saying "Data too long for column ...". I'm actually trying to load the rich text box with a .rtf file that may contain text, images etc so the string becomes big. Do I need to change the database field from VARCHAR to BLOB? I tried changing it to TEXT but got the same result.


BLOB and TEXT store the same amount of data. BLOB just treats it as binary, while TEXT treats it as, you guessed it, TEXT. Both are limited to 65535 bytes (as is VARCHAR). I'd use the text variants (instead of the blob variants). MEDIUMTEXT gives you 16,777,215 characters while LONGTEXT gives you 4,294,967,295 characters. Replace the TEXT part with BLOB if you want to use the blob types.


I changed the database field to LONGTEXT and it seems to be working fine. The only thing is that as the .rtf file becomes bigger (with more images), saving and restoring is taking too long. Any idea on how to make it more efficient?


I`m afraid not. There is plenty of data to save. You can create a progressBar, which will show the progress of saving, and create a new thread which will do the saving (and restoring data back to richTextBox).
Btw: but even the type varchar should do it. Maybe you didn`t set the lenght long enough?

Edited by Mitja Bonca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.