How to open a CrystalReport in ASP.Net C#?

S

Scott Roberts

Jason Huang said:
Hi,

Would someone tell me how to open a CrystalReport in my ASP.Net C#?
We can use the Show method to open a windows form,
but what method should we use for opening a CrystalReport?

Here's some "quick & dirty" code. There are a LOT of things wrong with this
code, so please use it as an example only!!

There's a "selection page" that retrieves the data and loads the report (if
you use the "pull" method of Crystal Reports you wouldn't need to retrieve
the data). The selection page then redirects to a generic "viewer page" that
sends appropriate output back to the client browser.

Selection page:

private void btnSummaryReport_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
// Retrieve the report data.
string tSQL = "select PaymentMethod, sum(TotalPriceAdult) as
TotalPriceAdult "
+ " from TicketMaster"
+ " where MasterNumber > '0'";

if ( BeginningDate.Text.Trim() != string.Empty )
tSQL += " and TransactionDate >= '"+BeginningDate.Text+"' ";
if ( EndingDate.Text.Trim() != string.Empty )
tSQL += " and TransactionDate <= '"+EndingDate.Text+"' ";
if ( Location.SelectedValue.Trim() != "A" )
tSQL += " and Location = '"+Location.SelectedValue+"' ";
if ( UserCode.SelectedValue.Trim() != "A" )
tSQL += " and UserId = '"+UserCode.SelectedValue+"' ";

tSQL += " group by PaymentMethod order by PaymentMethod ";

SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["SqlConnectionString"]);
SqlDataAdapter data = new SqlDataAdapter(tSQL,conn);
DataSet ds = new DataSet();
data.Fill(ds);

// Set up the Crystal Report objects.
CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new
CrystalDecisions.CrystalReports.Engine.ReportDocument();
rpt.Load(System.Configuration.ConfigurationSettings.AppSettings["ReportDirectory"]+"RevenueSummary.rpt");
rpt.SetDataSource(ds.Tables[0]);

// Session variables required for the report viewer page.
Session["ReportSource"] = rpt;
Session["ReportDataSet"] = ds;
Response.Redirect("ReportViewer.aspx?ReportType="+ReportType.SelectedValue,true);
}

In the "Report Viewer" page, there's this:

private void Page_Load(object sender, System.EventArgs e)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument tDoc =
(CrystalDecisions.CrystalReports.Engine.ReportDocument)Session["ReportSource"];
_reportType = Request.Params["ReportType"];
if ( tDoc != null )
{
CrystalReportViewer1.ReportSource = tDoc;
if ( _reportType == "XLS" )
ExportToExcel();
else if ( _reportType == "XLSRAW" )
ExportRawToXLS();
else if ( _reportType == "XML" )
ExportToXML();
else if ( _reportType == "CSV" )
ExportToCSV();
else
ExportToPDF();
}
}

I've never found the Crystal web report viewer (that outputs HTML) very
useful. We default everything to PDF. I guess if you wanted to use the
"drill down" functionality the built in HTML viewer might be nice.

private void ExportToPDF()
{
string _filename =
System.Configuration.ConfigurationSettings.AppSettings["TempDirectory"] +
Session.SessionID.ToString() + ".pdf";
DiskFileDestinationOptions crDiskFileDestinationOptions = new
DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName = _filename;
CrystalDecisions.CrystalReports.Engine.ReportDocument tDoc =
(CrystalDecisions.CrystalReports.Engine.ReportDocument)Session["ReportSource"];
CrystalDecisions.Shared.ExportOptions crExportOptions =
tDoc.ExportOptions;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
tDoc.Export();

// The following code writes the pdf file
// to the Client's browser.
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.WriteFile(_filename);
Response.Flush();
Response.Close();

// delete the exported file from disk
System.IO.File.Delete(_filename);
}
 
J

Jason Huang

Hi,

Would someone tell me how to open a CrystalReport in my ASP.Net C#?
We can use the Show method to open a windows form,
but what method should we use for opening a CrystalReport?
Thanks for help.


Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top