Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing data of rich text box to database with formatting

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!

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);
    }
like image 509
Amine Da. Avatar asked Apr 13 '13 01:04

Amine Da.


People also ask

How do you store rich text data in a database?

You can store rich, formatted text in an Access database by using a Long Text (also called Memo) field and setting the field's TextFormat property to RichText. For example, you can make the text bold or underlined, apply different fonts to individual words or characters, and change text colors.

Does rich text allow formatting?

If you enable rich-text formatting for a rich text box, users can use a variety of options to format the text that they enter in that control. For example, they can apply a different font or character style to the text inside the rich text box or even insert a table into the rich text box.


1 Answers

To get the formatted text that will be saved in the db:

string rtfText; //string to save to db
TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    rtfText = Encoding.ASCII.GetString(ms.ToArray());
}

To restore the formatted text retrieved from the db:

string rtfText= ... //string from db
byte[] byteArray = Encoding.ASCII.GetBytes(rtfText);
using (MemoryStream ms = new MemoryStream(byteArray))
{
    TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
    tr.Load(ms, DataFormats.Rtf);
}

You can also use XAML format instead, using DataFormats.XAML on load an save.

like image 159
buylar Avatar answered Oct 16 '22 07:10

buylar