Loading a report in Web Forms

1

I want to generate some reports in my application and I'm using Crystal Reports. The Report itself is correct but I can not load it on my page (.aspx).

Here is my application's Load event code:

protected void Page_Load(object sender, EventArgs e)
{
    var doc = new ReportDocument();

    doc.Load(MapPath("~/Relatorios/MeuRelatorio.rpt"));
    doc.SetDatabaseLogon("my_user","senha123");

    this.CrystalReportViewer1.ReportSource = doc;
    this.CrystalReportViewer1.PrintMode = PrintMode.Pdf;
    this.CrystalReportViewer1.RefreshReport();
}

The code does not display anything on the page, but debugging I was able to see all the results entered in the variable doc . I was able to see your lines, result of login success and etc.

I know that in MVC I can give Return Action with File , and File > I export the report result, eg:

public ActionResult Relatorio()
{
    //Restante do código

    //Exporta em pdf à variável "stream"
    Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    rptH.Refresh();
    //Retorna o tipo File
    return File(stream, "application/pdf");
}

But in Web Forms I have no idea how I extract this information from the screen.

    
asked by anonymous 08.04.2014 / 22:18

2 answers

1

I found what was missing from my code. You need to use the ExportToHttpResponse () method of the ReportDocument () class in which you ask for some parameters like:

  • Export type (pdf, xls, txt ..)
  • The Response
  • A boolean indicating whether it will be exported in download or not
  • The name of the report

Example:

protected void Page_Load(object sender, EventArgs e)
{
    var doc = new ReportDocument();

    doc.Load(MapPath("~/Relatorios/MeuRelatorio.rpt"));
    doc.SetDatabaseLogon("my_user","senha123");

    doc.Refresh();
    doc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, MapPath("~/Relatorios/MeuRelatorio.rpt"));
}
    
08.04.2014 / 23:30
0

So:

Response.AddHeader("Content-Disposition", "attachment;.filename=SeuReport.pdf");
// Response.AddHeader("Content-Length", coloque aqui o tamanho do arquivo em bytes);
Response.ContentType = "Application/octet-stream";
Response.BinaryWrite(rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
rptH.Refresh();
Response.End();

I did not test this code, but I believe that with some modifications you can achieve the expected result.

    
08.04.2014 / 22:46