Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
See more: , +
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. I've attached some code below.

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.

Here is my code.

What I have tried:

C#
namespace simpleton   
 public partial class AddNewOE : Window   
   {   
      //db connection           
      simpletonDBEntities _db = new simpletonDBEntities();  
      public AddNewOE()  
      {  
         InitializeComponent();  
       }  
  
       private void insertobBtn_Click(object sender, RoutedEventArgs e)  
       {  
   
           db_entry newdb_entries = new db_entry()   
          {   
             ReportDetails = ConvertRtbToBytes(rtfText)  
           };               
            _db.ob_entries.Add(newOb_entries);           
           _db.SaveChanges();   
        }  
       public static byte[] ConvertRtbToBytes(string richTextBox)   
       {   
            byte[] data = null; 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());  
             }   
            return data;  
         }  
     }  
  
}  
Posted
Updated 9-Oct-18 0:31am
Comments
Herman<T>.Instance 9-Oct-18 6:24am    
How does your XAML look like?

1 solution

Your function ConvertRtbToBytes(string richTextBox) asks for a string but ain't you needing the RichtTextBox object there in stead of the string? That Object holds the .Document property. A string does not have that
 
Share this answer
 
Comments
alvinchesaro 9-Oct-18 6:34am    
You're right! I changed that to the name property of my textbox and it worked!

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