0

I am using the Devexpress control: aspxichedit following this source code: https://github.com/DevExpress-Examples/how-to-use-aspxrichedit-to-edit-rtf-data-in-aspxgridviews-editform-t260978/blob/15.1.5%2B/CS/Default.aspx.cs

and at the code:

protected void re_Init(object sender, EventArgs e) {
    ASPxRichEdit richEdit = sender as ASPxRichEdit;
    GridViewEditItemTemplateContainer container = richEdit.NamingContainer as GridViewEditItemTemplateContainer;

    string documentID = GetDocumentID(container.Grid);
    if (!OpenedCanceledDocumentIDs.Contains(documentID)) {
        OpenedCanceledDocumentIDs.Add(documentID);
    }

    if (container.Grid.IsNewRowEditing) {
        richEdit.DocumentId = documentID;
        return;
    }

    //for text in db
    string rtfText = container.Grid.GetRowValues(container.VisibleIndex, "RtfContent").ToString();

    //for binary in db
    //byte[] rtfBinary = (byte[])container.Grid.GetRowValues(container.VisibleIndex, "RtfContent");

    richEdit.Open(documentID, DocumentFormat.Rtf, () => {
        //for text in db
        return Encoding.UTF8.GetBytes(rtfText);

        //for binary in db
        //return rtfBinary;
    });
}

At return Encoding.UTF8.GetBytes(rtfText);

I kept getting the error 'Cannot implicity convert byte[] to System.IO.Stream' when all online documentations uses byte[] to open the document in the control.

What could be happening? Why is my byte array not being accepted?

LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50

1 Answers1

1

The problem is occurred because the third parameter of ASPxRichEdit.Open() expects return type of System.IO.Stream type as provided in this reference, but you're passing byte array with GetBytes():

public void Open( 
   string documentId,  
   DocumentFormat format,  
   Func<Stream> contentAccessorByStream // => requires return type of stream
)

To fix this issue, try to convert that byte array to stream before returning it as in example below:

richEdit.Open(documentID, DocumentFormat.Rtf, () => {
    //for text in db
    var byteArray = Encoding.UTF8.GetBytes(rtfText);
    var stream = new MemoryStream(byteArray); // convert to stream

    return stream;
});

Related issue:

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61