Loading a report in Web Forms

Asked

Viewed 957 times

1

I intend to generate some reports in my application and I am using Crystal Reports. The Report itself is correct but I can’t load it on my page (.aspx).

Follow the Load event code of my application:

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 show anything on the page, but debugging I was able to see all the results inserted in the variable doc. I could see your lines, login success result and etc.

I know that in MVC I can give a Return in Action with the guy File, and in the File i export the report result, example:

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 to extract this information on the screen.

2 answers

1


I found what was missing in my code. It is necessary to use the method Exporttohttpresponse() class Reportdocument() in which it asks for some parameters such as:

  • The export type (pdf, xls, txt..)
  • The Response
  • A boolean that indicates whether it will be exported as a 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"));
}

0

Thus:

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 get the expected result.

  • Thank you @Cigano! You did not solve, but gave a light. The solution is published.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.