Data Cogs Information Technology

posts - 131, comments - 251, trackbacks - 36

General

Search This Site

Powered by Google

Brisbane

Locations of visitors to this page

Information

Archives

Images

Blogs

CV etc.

Links For Me

I don't know how many times I have gone hunting for this snippet over the past couple of years.  This function encapsulates passing a dataset (potentially made up of many DataTables) into a Crystal Report and then streaming the report back to a browser as a PDF file.  It has been very useful to me.  I hope you find it useful as well.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;

class MyReports
{
 public static void GenerateCrystalReportsPDF(
     string rptPath,
     string targetFileName,
     DataSet dsSource)
 {
  HttpResponse res;
  HttpContext ctx;
   
  // get the current HTTP Context the object is
  //running in
  ctx = HttpContext.Current;
  if ( ctx != null )
  {
   res = ctx.Response;
   if ( res == null )
   {
    return;
   }
  }
  else
  {
   return;
  }
  
  
  string timeStamp =
   DateTime.Now.ToString("yyyymmddhhnnss");
  FileStream fs;
  int FileSize;
  ReportDocument oRD = new ReportDocument();
  ExportOptions oExo = new ExportOptions();
  DiskFileDestinationOptions oExDo = new DiskFileDestinationOptions();

  oRD.Load(rptPath);
  oRD.SetDataSource(dsSource.Tables[0]);
   
  //check if more than one table - if so assign to subreport

  if (dsSource.Tables.Count>1)
  {
   //Go through each sections in the main report and identify the subreport by name
   Sections crSections = oRD.ReportDefinition.Sections;
   ReportObjects crReportObjects;
   SubreportObject crSubreportObject;
   ReportDocument crSubReportDoc;
   int subReportCount = 0;
   foreach(Section crSection in crSections)
   {
    crReportObjects = crSection.ReportObjects;
    //loop through all the report objects to find all the subreports
     
    foreach(ReportObject crReportObject in crReportObjects)
    {
     if (crReportObject.Kind == ReportObjectKind.SubreportObject)
     {
      //you will need to typecast the reportobject to a subreport
      //object once you find it
      subReportCount++;
      crSubreportObject = (SubreportObject)crReportObject;
      //open the subreport object
      crSubReportDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
      //Once the correct subreport has been located pass it the
      //appropriate dataset
      crSubReportDoc.SetDataSource(dsSource.Tables[subReportCount]);
     }
    }
   }
  }
  oExDo.DiskFileName = targetFileName;
  oExo = oRD.ExportOptions;
  oExo.ExportDestinationType =
  ExportDestinationType.DiskFile;
  oExo.ExportFormatType =
    ExportFormatType.PortableDocFormat;
  oExo.DestinationOptions = oExDo;
  oRD.Export();
  oRD.Close();

  //stream the file
  res.Clear();
  res.ClearContent();
  res.ClearHeaders();
  res.Buffer = true;
  res.ContentType = "application/pdf";
  fs = new FileStream(targetFileName, FileMode.Open);
  FileSize = (int) fs.Length;
  byte [] bBuffer;
  bBuffer = new byte[FileSize];
  fs.Read(bBuffer, 0, FileSize);
  fs.Close();
  res.BinaryWrite(bBuffer);
  res.Flush();
  res.Close();

  System.IO.File.Delete(targetFileName);
 }
}

 

posted on Monday, October 04, 2004 8:05 PM

Feedback

# re: Dynamically Pass a DataSet into a Crystal Report and Stream as PDF

It works on my subreport.
1/20/2005 10:32 PM | Jerry

# re: Dynamically Pass a DataSet into a Crystal Report and Stream as PDF

Thank you so much.. It helped me a lot .
5/17/2008 5:46 AM | Ragha

# re: Dynamically Pass a DataSet into a Crystal Report and Stream as PDF

Thank you so much.. It helped me a lot .
5/17/2008 5:46 AM | Ragha

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: