469,160 Members | 1,915 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

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

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
Nov 17 '05 #1
1 2854

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:u9**************@TK2MSFTNGP15.phx.gbl...
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.ReportDocum ent rpt = new
CrystalDecisions.CrystalReports.Engine.ReportDocum ent();
rpt.Load(System.Configuration.ConfigurationSetting s.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.ReportDocum ent tDoc =
(CrystalDecisions.CrystalReports.Engine.ReportDocu ment)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.AppSett ings["TempDirectory"] +
Session.SessionID.ToString() + ".pdf";
DiskFileDestinationOptions crDiskFileDestinationOptions = new
DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName = _filename;
CrystalDecisions.CrystalReports.Engine.ReportDocum ent tDoc =
(CrystalDecisions.CrystalReports.Engine.ReportDocu ment)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);
}
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by s-galit | last post: by
reply views Thread by s-galit | last post: by
reply views Thread by s-galit | last post: by
2 posts views Thread by Vlatko | last post: by
2 posts views Thread by Don Wash | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.