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.
![](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/Untitled2.png)
Step 3
Note: In this Table Adapter the column name must be the same as the database table fields.
Step 4
![](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/3.png)
Step 5
![](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/4.png)
Step 6
![DataTableAdapter](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/DataTableAdapter.jpg)
Click Next
![move field](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/move%20field.jpg)
Choose the field to display.
![click finish](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/click%20finish.jpg)
Choose Report Display Format.
Click Finish.
Step 7
![Design crystal report](https://www.csharp.com/admin/print-crystal-report-at-client-side-printer-in-Asp-Net03092019062807/Images/Design%20crystal%20report.jpg)
Finally your Design Page is ready as in the image.
Step 8
Step 9
Step 10
Code Behind
For the preview of Crystal Report Data:
- using System.Data.SqlClient;
- using System.Data;
- using CrystalDecisions.CrystalReports.Engine;
- using System.Configuration;
- public partial class ReportInvoice: System.Web.UI.Page {
- SqlDataAdapter sqlda = new SqlDataAdapter();
- SqlCommand com = new SqlCommand();
- DataTable dt;
- PrintDataSet1 ds = new PrintDataSet1();
- ReportDocument rptDoc = new ReportDocument();
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- bindCrystal();
- }
- }
- public void bindCrystal() {
- SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
- dt = new DataTable();
- dt.TableName = "Crystal Report Example";
- com.Connection = conn;
- string sql = "SELECT * from Table1 "; // As per condition
- com.CommandText = sql;
- sqlda = new SqlDataAdapter(com);
- sqlda.Fill(dt);
- ds.Tables[0].Merge(dt);
- rptDoc.Load(Server.MapPath("PrintCrystalReport.rpt"));
- rptDoc.SetDatabaseLogon("Username", "password", @"IPADDRESS\SERVERR2", "DB_DatabaseName");
- rptDoc.SetDataSource(ds);
- CrystalReportViewer1.ReportSource = rptDoc;
- }
- }
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.
- <input id="btnPrint" type="button" value="Print" onclick="Print()" />
- <div id="dvReport">
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"AutoDataBind="true" ToolPanelView="None" EnableDatabaseLogonPrompt="False"PrintMode="Pdf" />
- </div>
- <script type="text/javascript">
- function Print() {
- var dvReport = document.getElementById("dvReport");
- var frame1 = dvReport.getElementsByTagName("iframe")[0];
- if (navigator.appName.indexOf("Internet Explorer") != -1 || navigator.appVersion.indexOf("Trident") != -1) {
- frame1.name = frame1.id;
- window.frames[frame1.id].focus();
- window.frames[frame1.id].print();
- } else {
- var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
- frameDoc.print();
- }
- }
- </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.