Home > Microsoft .Net Development Tips > ASP.NET Development > Use ASP.NET to launch a report as a .pdf
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ASP.NET DEVELOPMENT

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


Jacob Behounek
03.15.2004
Rating: -4.45- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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!

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ASP.NET Development
Dundas Map for .NET kicks up geographic visualization
Return to CodePlex: Into the Sandcastle…
VBScript Tutorial
Use PHP with Visual Studio to create Web sites
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Visual Studio 2008 and .NET Framework 3.5 SP1 introduces ADO .NET Entity Designer
XAML as an everywhere presentation format
Introduction to Silverlight 1.0: Building your first Silverlight application
Creating custom ASP.NET 2.0 profile providers
Book excerpt: Silverlight 1.0 explained

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.

About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts