Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
I am new at wpf and I want to store the data of the rich text box along with its formatting (Italic, colored, Bold..) into a database (Mysql).

Currently when i save the data, formatting is ignored.

In addition, it shows all the text in the same line when i load it back to the rich text box from the database. Looking forward to your help and suggestions!

[EDIT #1 - moved from comment]

C#
public void save()
    {  

        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();      
        string richText = new TextRange(rt1.Document.ContentStart,  rt1.Document.ContentEnd).Text;

        string s = WebUtility.HtmlEncode(richText); 
        command.Parameters.AddWithValue("@s", s);           
        command.CommandText = "insert into proc_tra (procedures) values (@s)";
        conn.Open();
        command.ExecuteNonQuery();
        conn.Close();
    }

public void load()

    {   MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "select * from proc_tra where id_pt=4";
        rt1.Document.Blocks.Clear();            
        conn.Open();            
        MySqlDataReader dr;
        dr = command.ExecuteReader();
        string k="";           
        while (dr.Read())
        {              
            k += dr["procedures"].ToString();
        }
        var p = new Paragraph();
        var run = new Run();
        run.Text = WebUtility.HtmlDecode(k);
        p.Inlines.Add(run);
        rt1.Document.Blocks.Add(p);
    }
Posted
Updated 13-Apr-13 12:37pm
v4
Comments
Sergey Alexandrovich Kryukov 12-Apr-13 23:59pm    
There are too many ways to screw up things. Please, make a short code sample. What do you do to write RTF to stream and read from stream?
—SA
devillspdr 13-Apr-13 17:41pm    
From OP:
Question has been updated.
Sergey Alexandrovich Kryukov 13-Apr-13 19:25pm    
Why did you HTML-encode RTF? This is not the only problem, but why? There is no need to do it.
Is it System.Windows.Controls.RichTextBox (you don't show this most important declaration)?
—SA

You can consider the document as a BLOB and store it in database using file stream storage. Please see:
http://technet.microsoft.com/en-us/library/bb933993.aspx[^],
http://www.mssqltips.com/sqlservertip/1489/using-filestream-to-store-blobs-in-the-ntfs-file-system-in-sql-server-2008/[^].

On WPF side, you need to represent your rich text as the array of bytes which you can store or load to/from stream. Please see my past answer: save richtextbox in wpf c#[^].

—SA
 
Share this answer
 
RTF Data field should be "Memo".
private void InsertToMemo()
{
using (OleDbConnection oleDbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\AD.mdb"))
{
OleDbCommand oleDbCmd = new OleDbCommand("insert into Table2 values(1,'" + this.richTextBox1.Rtf + "')", oleDbConn);
oleDbCmd.Connection.Open();
oleDbCmd.ExecuteNonQuery();
}
}

private void ReadFormMemo()
{
using (OleDbConnection oleDbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\AD.mdb"))
{
OleDbCommand oleDbCmd = new OleDbCommand("select Field1 from Table2", oleDbConn);
oleDbCmd.Connection.Open();
OleDbDataReader oleDbDataReader = oleDbCmd.ExecuteReader();
oleDbDataReader.Read();
this.richTextBox2.Rtf = oleDbDataReader.GetString(0);
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900