Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to read to contents of the rich text box (which includes images) i stored my database but i keeps returning an exception.

"An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll Additional information: File format is not valid."
C#
cmd=new SqlCommand("select question from questions where id=1",connx);
 SqlDataReader dr = cmd.ExecuteReader();
 if (dr.Read())
 {
      Byte[] rtf = new Byte[Convert.ToInt32(dr.GetBytes(0, 0, null, 0, Int32.MaxValue))];

      long bytesReceived = dr.GetBytes(0, 0, rtf, 0, rtf.Length);

      ASCIIEncoding encoding= new ASCIIEncoding();

      question.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
 }
Posted
Comments
George Jonsson 18-Oct-15 21:09pm    
Maybe try to remove Convert.ToInt32 from the line Byte[] rtf = new Byte[Convert.ToInt32(dr.GetBytes(0, 0, null, 0, Int32.MaxValue))];
Member 9963673 18-Oct-15 22:13pm    
Not working sir but if i change the question.rtf at the last line to question.Text it loads the text only not the image
sreeyush sudhakaran 19-Oct-15 1:14am    
What is the datatype of question in database? Is that Image or text or binary?
Member 9963673 19-Oct-15 16:35pm    
Image Sir

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