locked
Set LocalReport of reportviewer from byte[] RRS feed

  • Question

  • User-1787558873 posted

    Hi, is it possible to set a  repportviewer from bytearray ?

    here is my service  :

    public class ReportServiceManager
        {
            private LocalReport Report;
    
            public byte[] RenderToPdf()
            {
                string templateName = "Reporting.Common.ReportTemplates.MyReport.rdlc";
                Report.SetTemplate(templateName, null);
                // set parameters
                // ....................
                // Set datasources
                // ....................
                string mimeType, encoding, fileNameExtension;
                string[] streams;
                Warning[] warnings;
                return Report.Render("PDF", GetPdfDeviceInfo(), out mimeType,
                    out encoding, out fileNameExtension, out streams, out warnings);
            }
    
            protected string GetPdfDeviceInfo()
            {
                string deviceInfo =
               "<DeviceInfo>" +
               "  <OutputFormat>" + "id" + "</OutputFormat>" +
               "  <PageWidth>8.5in</PageWidth>" +
               "  <PageHeight>11in</PageHeight>" +
               "  <MarginTop>0.5in</MarginTop>" +
               "  <MarginLeft>1in</MarginLeft>" +
               "  <MarginRight>1in</MarginRight>" +
               "  <MarginBottom>0.5in</MarginBottom>" +
               "</DeviceInfo>";
                return deviceInfo;
            }
        }
    
    

    So on my aspx page I do as follow :

    ReportServiceManager ReportServiceManager = new ReportServiceManager();
    var reportdata = ReportServiceManager.RenderToPdf();
    MemoryStream ms = new MemoryStream(reportdata);
    ReportViewer1.LocalReport.LoadReportDefinition(ms);
    ReportViewer1.DataBind();

    So my reportservice returns a byte[] and I want to use it to populate my report viewer

    Best regards

    Sunday, September 18, 2016 8:35 PM

Answers

  • User-2057865890 posted

    Hi Olivier,

    I do not want to send it to client for download.

    /* Uncomment to handle the file as attachment
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
    */

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 28, 2016 9:31 AM

All replies

  • User-2057865890 posted

    Hi Olivier,

    Take a look at this thread, you could try this

    private void CreatePDF(string fileName)
    {
        // Setup DataSet
        MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere();
    
    
        // Create Report DataSource
        ReportDataSource rds = new ReportDataSource("MyDataSourceName", ds.GetData());
    
    
        // Variables
        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
      
    
        // Setup the report viewer object and get the array of bytes
        ReportViewer viewer = new ReportViewer();
        viewer.ProcessingMode = ProcessingMode.Local;
        viewer.LocalReport.ReportPath = "YourReportHere.rdlc";
        viewer.LocalReport.DataSources.Add(rds); // Add datasource here
      
    
        byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
      
    
        // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = mimeType;
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
        Response.BinaryWrite(bytes); // create the file
        Response.Flush(); // send it to the client to download
    }

    Best Regards,

    Chris

    Monday, September 19, 2016 1:33 PM
  • User-1787558873 posted

    Hi, thank for your reply but I do not want to send it to client for download.

    I want the client to display it on a report viewer.

    Best regards

    Monday, September 19, 2016 8:18 PM
  • User-2057865890 posted

    Hi Olivier,

    I do not want to send it to client for download.

    /* Uncomment to handle the file as attachment
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
    */

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 28, 2016 9:31 AM