Print Crystal Report at Client Side Printer in ASP.NET

The following is the procedure.

Note: To use Crystal Reports in Visual Studio 2010, you need to download CRforVS_13_0.exe and install it.

Step 1

Create a new Dataset in your existing project.

In Solution Explorer select Add New Item, then Dataset1.


Step 2
 
In Dataset Add, then click Table Adapter.



Step 3
 
In this Table Adapter, Add Columns depending on the Database Tables fields.

Note: In this Table Adapter the column name must be the same as the database table fields.

Step 4
 
Add New Item, then select Crystal Reports.



Step 5
 
In the Crystal Reports Field Explorer Menu select Database Fields -> Database Expert.



Step 6
 
Choose the previously created Dataset from the List then drag any DataTableAdapter to the right side.

DataTableAdapter

Click Next

move field

Choose the field to display.

click finish

Choose Report Display Format.

Click Finish.

Step 7
 
After binding with the Dataset, design the Crystal Reports report by dragging fields one by one to the Crystal Reports report.

Design crystal report

Finally your Design Page is ready as in the image.

Step 8
 
Now select Add New Item and name it NewWebform.aspx.

Step 9
 
Add the CrystalReportViewer to NewWebform.aspx. Do not choose a Report Source.

Step 10
 
Now write code to view the data in Crystal Reports depending on your condition.

Code Behind

For the preview of Crystal Report Data:
  1. using System.Data.SqlClient;  
  2. using System.Data;  
  3. using CrystalDecisions.CrystalReports.Engine;  
  4. using System.Configuration;  
  5. public partial class ReportInvoice: System.Web.UI.Page {  
  6.     SqlDataAdapter sqlda = new SqlDataAdapter();  
  7.     SqlCommand com = new SqlCommand();  
  8.     DataTable dt;  
  9.     PrintDataSet1 ds = new PrintDataSet1();  
  10.     ReportDocument rptDoc = new ReportDocument();  
  11.     protected void Page_Load(object sender, EventArgs e) {  
  12.         if (!IsPostBack) {  
  13.             bindCrystal();  
  14.         }  
  15.     }  
  16.     public void bindCrystal() {  
  17.         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);  
  18.         dt = new DataTable();  
  19.         dt.TableName = "Crystal Report Example";  
  20.         com.Connection = conn;  
  21.         string sql = "SELECT * from Table1 "// As per condition  
  22.         com.CommandText = sql;  
  23.         sqlda = new SqlDataAdapter(com);  
  24.         sqlda.Fill(dt);  
  25.         ds.Tables[0].Merge(dt);  
  26.         rptDoc.Load(Server.MapPath("PrintCrystalReport.rpt"));  
  27.         rptDoc.SetDatabaseLogon("Username""password", @"IPADDRESS\SERVERR2""DB_DatabaseName");  
  28.         rptDoc.SetDataSource(ds);  
  29.         CrystalReportViewer1.ReportSource = rptDoc;  
  30.     }  
  31. }  

Finally, the Print Preview is done in the Crystal Report Viewer.

Procedure to Print this Report from a client-side printer.

UI Design Side

Step 11

Add a HTML button (Print button) to the Webform, not asp:Button.

  1. <input id="btnPrint" type="button" value="Print" onclick="Print()" />  
Put Crystal ReportViewer in a <Div> tag as in the following:
  1. <div id="dvReport">  
  2.    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"AutoDataBind="true" ToolPanelView="None" EnableDatabaseLogonPrompt="False"PrintMode="Pdf" />  
  3. </div>  
Use JavaScript in a Design Page as in the following:
  1. <script type="text/javascript">  
  2.     function Print() {  
  3.         var dvReport = document.getElementById("dvReport");  
  4.         var frame1 = dvReport.getElementsByTagName("iframe")[0];  
  5.         if (navigator.appName.indexOf("Internet Explorer") != -1 || navigator.appVersion.indexOf("Trident") != -1) {  
  6.             frame1.name = frame1.id;  
  7.             window.frames[frame1.id].focus();  
  8.             window.frames[frame1.id].print();  
  9.         } else {  
  10.             var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;  
  11.             frameDoc.print();  
  12.         }  
  13.     }  
  14. </script>  

Step 12

Browser Side

Now you can run the report in the browser and see the following:



Finally, the printing is done perfectly.

Note: If you want to change the printer then click on the Change Button and you can save this file as a PDF from this option.



Note: If you want to not print the header and footer URL and PageNo and Page1of1, then UncheckHeaders and Footers option.

I hope you liked my article. If you have any query regarding this, feel free to comment for me.


Similar Articles