ASP.NET - print button code for c# asp.net

Asked By Chinnarasu Chinnarasu.M on 23-Oct-11 06:31 AM
 i set print button in crystal report page ... i need to click button mean print a crystal report .. any body help me...
Suchit shah replied to Chinnarasu Chinnarasu.M on 23-Oct-11 07:27 AM
You could use simple window.print through javascript and as well as from server side as discussed here
http://aspalliance.com/509_Automatically_Printing_Crystal_Reports_in_ASPNET.all


Suchit shah replied to Chinnarasu Chinnarasu.M on 23-Oct-11 07:37 AM
 ReportDocument rptDoc = new ReportDocument();

   
ReportProjectProperties ds = new ReportProjectProperties();// .xsd file name
   
DataTable dt = DbUtility.GetProjectWorksheets(15);
    ds
.Tables[0].Merge(dt);
   
// Your .rpt file path will be below
    rptDoc
.Load(Server.MapPath("../Reports/report1.rpt"));
   
//set dataset to the report viewer.
    rptDoc
.SetDataSource(ds);
   
CrystalReportViewer1.ReportSource = rptDoc; Even if u can look at http://www.codeproject.com/KB/aspnet/crCode.aspx
sam sam replied to Chinnarasu Chinnarasu.M on 23-Oct-11 07:50 AM
Take a look at this thread, seems to ask a similar question.  http://forums.asp.net/p/1561011/3861081.aspx
sam sam replied to Chinnarasu Chinnarasu.M on 23-Oct-11 07:51 AM


This control allows you to put a 'Print' button on any web form to bring up the 'Print' dialog.

View ASPX Source 
<%@ Page language="c#" MasterPageFile="~/MasterPages/Unbranded.master" %>
<%@ Register TagPrefix="snaplets" Namespace="OpinionatedGeek.Applications.Snaplets" Assembly="OpinionatedGeek.Applications.Snaplets"%>
<%@ Register TagPrefix="opgeek" Namespace="OpinionatedGeek.Web.PowerPack" Assembly="OpinionatedGeek.Web.PowerPack"%>
<%@ Register TagPrefix="site" Namespace="OpinionatedGeek.Site" Assembly="App_code"%>
<asp:content id="BrowserTitleContent" contentplaceholderid="BrowserTitle" runat="server">
    A simple print button for web forms!
</asp:content>
<asp:content id="TitleContent" contentplaceholderid="Title" runat="server">
    ASP.NET PowerPack PrintButton Control
</asp:content>
<asp:content id="BodyContent" contentplaceholderid="Body" runat="server">
<p>
This control allows you to put a 'Print' button on any web form to bring up the 'Print' dialog.
</p>
<blockquote>
<opgeek:PrintButton id="butPrint" runat="server"/>
</blockquote>


<site:ViewSource runat="server"/>
</asp:content>
smr replied to Chinnarasu Chinnarasu.M on 23-Oct-11 08:18 AM
hi

You can print a Crystal Report using print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report direct to printer without viewing the report in Crystal Report Viewer.

The PrintToPrinter method takes four parameters.
  
   
  
nCopies : Indicates the number of copies to print. 
  
collated : Indicates whether to collate the pages. 
  
startPageN : Indicates the first page to print. 
  
endPageN : Indicates the last page to print. 
  
   
  
The following steps will guide you to achieve the same:
  
   
  
Add crystal report (.cr) file to your ASP.NET application. 
Add a report instance on the page level. 
  
Dim report As MyReport = New MyReport
   
  
Populate reports data on Page_Init  
  
  ' Get data in a DataSet or DataTable
    Dim ds As DataSet = GetData()
  
    ' Fill report with the data
  
   report.SetDataSource(ds)
  
   
  
Print Report 
report.PrintToPrinter(1, False, 0, 0)
   
  
If you wish to print certain page range, change last two parameters From to To page number. 
  
   
  
If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument. 
  
   
  
The following code sets page margins and printer name:
  
   
  
Dim margins As PageMargins =  Report.PrintOptions.PageMargins
  
   margins.bottomMargin = 200
  
   margins.leftMargin = 200
  
   margins.rightMargin = 50
  
   margins.topMargin = 100
  
   Report.PrintOptions.ApplyPageMargins(margins)
  
   
  
   ' Select the printer name
  
   Report.PrintOptions.PrinterName = printerName

refer
http://www.c-sharpcorner.com/UploadFile/mahesh/CRPrinting10062006161454PM/CRPrinting.aspx
http://stackoverflow.com/questions/749455/printing-crystalreport-reports-in-a-asp-net-application-without-using-pdf
Anoop S replied to Chinnarasu Chinnarasu.M on 24-Oct-11 02:15 AM

The ReportDocument class provides PrintToPrinter method that may be used to print a CR direct to the printer. If no printer is selected, the default printer will be used to send the printing pages to.

 

The PrintToPrinter method takes four parameters.

 

nCopies : Indicates the number of copies to print.

collated : Indicates whether to collate the pages.

startPageN : Indicates the first page to print.

endPageN : Indicates the last page to print.

 

The following steps will guide you to achieve the same:

 

  1. Add crystal report (.cr) file to your ASP.NET application.
  2. Add a report instance on the page level.

    Dim report As MyReport = New MyReport

 

  1. Populate reports data on Page_Init 

     
    ' Get data in a DataSet or DataTable

    Dim ds As DataSet = GetData()

    ' Fill report with the data

   report.SetDataSource(ds)

 

  1. Print Report
    report.PrintToPrinter(1, False, 0, 0)

 

If you wish to print certain page range, change last two parameters From to To page number.

 

If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument.

 

The following code sets page margins and printer name:

 

Dim margins As PageMargins =  Report.PrintOptions.PageMargins

   margins.bottomMargin = 200

   margins.leftMargin = 200

   margins.rightMargin = 50

   margins.topMargin = 100

   Report.PrintOptions.ApplyPageMargins(margins)

 

   ' Select the printer name

   Report.PrintOptions.PrinterName = printerName