Render Local Report on Button Click

Tags:

I needed to render a local report to pdf on a button click. This was not the first time i’ve done it without any problems but this time, the following problem was loaded…

Sys.WebForms.PageRequestManagerParserErrorException:  The message received from the server could not be parsed.  Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules or server trace is enabled.
Details:  Error parsing near ‘%PDF-1.3%
1 0 obj’.

 

Next is the coding i normally use to render the report…

private void RenderReport()

{

LocalReport localReport = new LocalReport();

localReport.ReportPath = Server.MapPath(“Reports/Daily.rdlc”);

source_Daily.DataBind();

//Give the collection a name (EmployeeCollection) so that we can reference it in our report designer

ReportDataSource reportDataSource = new ReportDataSource(“Analyze_ReportDaily”, source_Daily);

localReport.DataSources.Add(reportDataSource); string reportType = “PDF”;string mimeType;string encoding;

string fileNameExtension;

//The DeviceInfo settings should be changed based on the reportType

//http://msdn2.microsoft.com/en-us/library/ms155397.aspx

string deviceInfo = “<DeviceInfo>” +” <OutputFormat>PDF</OutputFormat>” +” <PageWidth>8.5in</PageWidth>” +” <PageHeight>11in</PageHeight>” +” <MarginTop>0.3in</MarginTop>” +” <MarginLeft>0.0in</MarginLeft>” +” <MarginRight>0.0in</MarginRight>” +” <MarginBottom>0.3in</MarginBottom>” +“</DeviceInfo>”; Warning[] warnings; string[] streams;byte[] renderedBytes;//Render the report

renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding,out fileNameExtension, out streams, out warnings);//Clear the response stream and write the bytes to the outputstream//Set content-disposition to “attachment” so that user is prompted to take an action

//on the file (open or save) Response.Clear();Response.ContentType = mimeType; Response.AddHeader(“content-disposition”, “attachment; filename=foo.” + fileNameExtension);

Response.BinaryWrite(renderedBytes);

Response.End(); 

}

 To solve the previous error, cut the following code:

Response.Clear();

Response.ContentType = mimeType;

Response.AddHeader(“content-disposition”, “attachment; filename=foo.” + fileNameExtension);

Response.BinaryWrite(renderedBytes);

Response.End();

And insert the next code: 

Session[“report”] = renderedBytes;

System.Text.StringBuilder _sb = new System.Text.StringBuilder();

_sb.Append( “window.open(‘../Report.aspx’,”,”);

_sb.Append(“‘toolbar=0,menubar=0,resizable=yes’)”);

ScriptManager.RegisterStartupScript(Page, Page.GetType(), “winOpen”, _sb.ToString(), true);

 Also create a new web page, name it Report.aspx and insert the following code in the Page_Load:

string extension = “PDF”;

Response.ContentType = “application/pdf”;

// set the MIME type here

Response.AddHeader(“content-disposition”, “attachment; filename=Test.” + extension);

Response.BinaryWrite((byte[])Session[“report”]);

This should solve the problem. It’s a bit long but that’s the only solution i found that works fine. If you find a simpler way, please let me know to render a report on button click, please let me know. I would be happy to update the coding.

Happy Reporting!! 🙂

Reference: http://forums.asp.net/p/1183238/2013836.aspx

Categories

10 Responses

  1. I came across the same problem, but I noticed that the button that i was trying to run the script from was within an update panel, which throws this error, outside it, it works perfectly.

    After searching long and hard, I found two solutions:

    1) move the button to be outside of the update panel

    2) add a “POSTBACK TRIGGER” (not AsyncPostback Trigger) … and add the button

    Hope that helps :o)

  2. Salam! I ve got the same problem but my button control is not within an update panel, I’m working with VS .net 2008! thanks

  3. i getting the error an error occured during local processing
    LocalReport localReport = new LocalReport();
    localReport.ReportPath = @”E:Raja PalanisamyNxGsrctrunkCommunicationReportWorkSheet.rdlc”;

    Microsoft.Reporting.WebForms.ReportDataSource rds = new ReportDataSource(“dtBasicPlanDetails”, dtBasicPlanDetails);
    Microsoft.Reporting.WebForms.ReportDataSource rds1 = new ReportDataSource(“dtBasicCoverage”, dtBasicCoverage);
    Microsoft.Reporting.WebForms.ReportDataSource rds2 = new ReportDataSource(“dtBasicPremium”, dtBasicPremium);
    Microsoft.Reporting.WebForms.ReportDataSource rds3 = new ReportDataSource(“dtUnitMultiplier”, dtUnitMultiplier);
    Microsoft.Reporting.WebForms.ReportDataSource rds4 = new ReportDataSource(“dtDependentPlanDetails”, dtDependentPlanDetails);
    Microsoft.Reporting.WebForms.ReportDataSource rds5 = new ReportDataSource(“dtDependentCoverage”, dtDependentCoverage);
    Microsoft.Reporting.WebForms.ReportDataSource rds6 = new ReportDataSource(“dtDependentPremium”, dtDependentPremium);
    Microsoft.Reporting.WebForms.ReportDataSource rds7 = new ReportDataSource(“dtDependentUnitMultiplier”, dtDependentUnitMultiplier);
    Microsoft.Reporting.WebForms.ReportDataSource rds8 = new ReportDataSource(“dtSuppliPlanDetails”, dtSuppliPlanDetails);
    Microsoft.Reporting.WebForms.ReportDataSource rds9 = new ReportDataSource(“dtSuppliCoverage”, dtSuppliCoverage);
    Microsoft.Reporting.WebForms.ReportDataSource rds10 = new ReportDataSource(“dtSuppliPremium”, dtSuppliPremium);
    Microsoft.Reporting.WebForms.ReportDataSource rds11 = new ReportDataSource(“dtSuppliUnitMultiplier”, dtSuppliUnitMultiplier);
    localReport.DataSources.Add(rds);
    localReport.DataSources.Add(rds1);
    localReport.DataSources.Add(rds2);
    localReport.DataSources.Add(rds3);
    localReport.DataSources.Add(rds4);
    localReport.DataSources.Add(rds5);
    localReport.DataSources.Add(rds6);
    localReport.DataSources.Add(rds7);
    localReport.DataSources.Add(rds8);
    localReport.DataSources.Add(rds9);
    localReport.DataSources.Add(rds10);
    localReport.DataSources.Add(rds11);

    //localReport.DataSources.Clear();
    string reportType = “PDF”;
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType

    string deviceInfo =
    “” +
    ” PDF” +
    ” 8.5in” +
    ” 11in” +
    ” 0.5in” +
    ” 1in” +
    ” 1in” +
    ” 0.5in” +
    “”;

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;

    //Render the report
    renderedBytes = localReport.Render(
    reportType,
    deviceInfo,
    out mimeType,
    out encoding,
    out fileNameExtension,
    out streams,
    out warnings);

    //Clear the response stream and write the bytes to the outputstream
    //Set content-disposition to “attachment” so that user is prompted to take an action
    //on the file (open or save)
    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader(“content-disposition”, “attachment; filename=foo.” + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    Response.End();

  4. Oh my goodness! Impressive article dude! Thanks, However
    I am going through issues with your RSS. I don’t understand the reason why I am unable to join it. Is there anyone else having similar RSS problems? Anybody who knows the solution can you kindly respond? Thanx!!

Leave a Reply

Your email address will not be published. Required fields are marked *