Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Add report from database into CrystalReportViewer in memory.

I have Crystal Reports saved in our database, and want to show the reports in the CrystalReportViewer on a windows form.

Is there a way to show the report in the viewer without saving the report first to the file system?

Now I have the following code:

            ...
            var fileByteArray = (byte[])_GetDataReplyMessage.DataSet.Tables[0].Rows[0]["RAPPORT"];

            var tempFileName = Path.GetTempFileName() + ".rpt";
            var saveOk = SaveFile(fileByteArray, tempFileName);
            this.crystalReportViewer.ReportSource = tempFileName;
            ...

      public static bool SaveFile(byte[] byteArray, string path, bool overwrite)
      {
         var saveFile = false;
         if (File.Exists(path) && !overwrite)
         {
            throw new IOException("File already exists");
         }

         // Write bytearray in parts to file system.
         // This will prevent al long lock of the file system resource.
         int part = 1024;
         int cursor = 0;
         using (var fileStream = new FileStream(path, FileMode.Create))
         {
            while (cursor < byteArray.Length)
            {
               byte[] bytesToWrite = byteArray.Skip(cursor).Take(part).ToArray();
               fileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
               cursor += part;
            }
            fileStream.Close();
         }
         saveFile = true;
         return saveFile;
      }

This way the report needs to be on the clients filesystem.

The crystalReportViewer.ReportSource property is of the type 'object', and I am wondering if I can set this property with something else than a filename. I tried to set the ReportSource property with a memorystream, but this does not seem to work.

Not what you were looking for? View more on this topic or Ask a question