Alvin Chesaro

Alvin Chesaro

  • NA
  • 87
  • 1.1k

How to Store RichTextBox to SQLServer Database in WPF

Oct 9 2018 4:05 AM

I'm a bit new to C# and WPF and experiencing a very frustrating issue storing a richtextbox with formatting to my database using entity framework.

Here is my code.

  1. namespace simpleton   
  2.  public partial class AddNewOE : Window   
  3.    {   
  4.       //db connection           
  5.       simpletonDBEntities _db = new simpletonDBEntities();  
  6.       public AddNewOE()  
  7.       {  
  8.          InitializeComponent();  
  9.        }  
  10.   
  11.        private void insertobBtn_Click(object sender, RoutedEventArgs e)  
  12.        {  
  13.    
  14.            db_entry newdb_entries = new db_entry()   
  15.           {   
  16.              ReportDetails = ConvertRtbToBytes(rtfText)  
  17.            };               
  18.             _db.ob_entries.Add(newOb_entries);           
  19.            _db.SaveChanges();   
  20.         }  
  21.        public static byte[] ConvertRtbToBytes(string richTextBox)   
  22.        {   
  23.             byte[] data = null; string rtfText; //string to save to db   
  24.            TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);   
  25.            using (MemoryStream ms = new MemoryStream())   
  26.            {               
  27.                    tr.Save(ms, DataFormats.Rtf);              
  28.                    rtfText = Encoding.ASCII.GetString(ms.ToArray());  
  29.              }   
  30.             return data;  
  31.          }  
  32.      }  
  33.   

Please help me debug it, I'm getting the error on the ConvertRtbToBytes Function that... 'string' does not contain definition for 'Document' and no accessible extension method 'Document' accepting a first argument of type string could be found (are you missing a using directive or an assembly reference?)

I am also sure the general code is not Okay, any help will be massively appreciated.


Answers (1)