I am populating data in Crystal Report using SQL Server stored procedures. I was able to achieve my desired printing output using parameters passing and formatting each column using formula in CR.
In printing reports, the usual process is it will preview the created / generated output in Crystal Report Viewer, then, there are the print, export options where it will convert first the report to PDF to proceed to the printing functions
What I want is when I click the print button, It will automatically lead to printing procedures.
I was lead to this link for the answer How to print crystal report directly to a client machine using C# Asp.net
using oReportDocument.PrintToPrinter(1,true,0,0);
code, others also suggesting to fill the dataset in page init, but I am seem lost on how to do it.
This is my currently working code (the usual printing process where it will lead you first to the Crystal Report preview.
public partial class Report_MonthlySalesReportPrint : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
ReportDocument report = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
//Parameters to be passed as needed in formulas in Report
string RP = Session["RP"].ToString();
DateTime cms = Convert.ToDateTime(Session["CurrentMonthStart"].ToString());
string Loc = Session["Location"].ToString();
string MonthCurrent = Session["MonthCurrent"].ToString();
string YearCurrent = Session["YearCurrent"].ToString();
string MonthPrevious = Session["MonthPrevious"].ToString();
string YearPrevious = Session["YearPrevious"].ToString();
string MonthLastYear = Session["MonthLastYear"].ToString();
string YearLastYear = Session["YearLastYear"].ToString();
report.Load(Server.MapPath("MSRC.rpt"));
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
CrystalReportViewer1.DataBind();
report.SetParameterValue(0, MonthLastYear);
report.SetParameterValue(1, MonthCurrent);
report.SetParameterValue(2, MonthPrevious);
report.SetParameterValue(3, RP);
report.SetParameterValue(4, Loc);
report.SetParameterValue(5, cms);
report.SetParameterValue(6, YearCurrent);
report.SetParameterValue(7, YearPrevious);
report.SetParameterValue(8, YearLastYear);
report.PrintToPrinter(1, true, 0, 0);
con.Close();
}
}
UPDATED:
I just needed this to update for clarification purposes that the accepted answer below will work only on server side. Meaning when user is accessing the server remotely, the code will not work.