Tuesday, June 16, 2009

How set parameter in Crystal Report and print to printer in ASP.NET

In the following example you can see how to set the report parameters programmatically :

ASP.NET, C# :

// Creating a new report document :
ReportDocument rptdoc = new ReportDocument();
rptdoc.Load(Server.MapPath("report name and path"));


//Setting the report parameter :
ParameterField field = rptdoc.ParameterFields[0];
ParameterDiscreteValue paraval = new ParameterDiscreteValue();
paraval.Value = "parameter value";
field.CurrentValues.Add(paraval);


//Setting the report source :
CrystalReportViewer1.ReportSource = rptdoc;

don't forget to use these libraries:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;



In a Web application the print button on the CrystalReportViewer control toolbar opens a secondary Web page that emulates a print dialog box. A pdf is generated on the Web server, and then downloaded and displayed on the Web page. If you want to print the report directly to your local printer, you must set CrystalReportViewer properties and also add some line of xml codes to your web.config file :

//On PageLoad method of your web page :
CrystalReportViewer.PrintMode = PrintMode.ActiveX;

add the following xml to your Web Site's web.config file :

<configSections>
<sectionGroup name="businessObjects">
<sectionGroup name="crystalReports">
<section name="printControl" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
<sectionGroup>
<configSections>

<businessObjects>
<crystalReports>
<printControl>
<add key="url" value="http://myserver/PrintControl.cab" />
<printControl>
<crystalReports>
<businessObjects>

Note:

Only Internet Explorer supports ActiveX controls. Printing from a non-Internet Explorer client (FireFox, Safari, Mozilla, and others) reverts to the PDF export dialog.

PrintControl.cab can be downloaded from here.

more info :

No comments:

Post a Comment