Tip

Use ASP.NET to launch a report as a .pdf

Please let other users know how useful this tip is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


This tip shows how to output a report at runtime that launches a new window with a .PDF version of the report.

This example assumes you have a .NET report named ReportFile included in the ASP.NET project. Create a blank ASP.NET form and add the data objects (data adapter, connection, dataset) to match the report file. Then set the page.load event to the following:


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        'Populate the data source of the report.
        Me.daDataAdapter.Fill(Me.dsDataSet1)
        Dim crReport As New ReportFile
        crReport.SetDataSource(dsDataSet1)
        'Change the output stream of the current form
        Dim strStream As New System.IO.BinaryReader(crReport.ExportToStream
(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))

        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(strStream.ReadBytes(strStream.BaseStream.Length))
        Response.Flush()
        Response.Close()

    End Sub

By redirecting to this form, it will populate the dataset, load the report as a .PDF and redirect that output stream to the client.

To have it launch in its own window, open the form with JavaScript. Often, I will have a form with a repeater control to display data from a database, and at the bottom, I'll have a button to display a printable version. That button opens the report Web form in a new window:


<INPUT onclick="javascript:window.open('PrintPage.aspx')" type="button"
value="View Printable Version">

If you need to supply a parameter, open the page with a querystring and read that value in the page.load event. Use a parameter item for the DataAdapter.Fill:


<INPUT onclick="javascript:window.open('PrintPage.aspx?Param=Hello')" 
type="button" value="View Printable Version">

PrintPage.aspx page.load event:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        'Get parameter value from querystring
        Dim tcParam As String
        tcParam = Me.Request.QueryString.Get("Param")
        Me.daDataAdapter.SelectCommand.Parameters("@tValue").Value = tcParam
        Me.daDataAdapter.Fill(Me.dsDataSet1)
        Dim crReport As New ReportFile
        crReport.SetDataSource(dsDataSet1)
        Dim strStream As New System.IO.BinaryReader(crReport.ExportToStream
(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat)

        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(strStream.ReadBytes(strStream.BaseStream.Length))
        Response.Flush()
        Response.Close()
    End Sub

I hope this helps spark some new ideas for using reports in ASP.NET. Thanks!

This was first published in March 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.