Print Crystal Report from own button in asp.net c#

12:11:00 AM Mahesh Kumar Yadav 0 Comments


C# code to print crystal report in asp.net from print button

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using SoftHMS.GUI.MISC; 
using SoftHMS.Layer.Business; 

namespace SoftHMS.GUI.Pharmacy.PharmacyReports 
{ 
public partial class breakageReport : System.Web.UI.Page 
{ 
DataSet ds = new DataSet(); 
CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

protected void Page_Load(object sender, EventArgs e) 
{ 
DateTime TodayDate = DateTime.Now; 
string todaydate = SoftHMS.GUI.NepDate.getNepaliDate(TodayDate).Replace("/", "-"); 
if (!Page.IsPostBack) 
{ 
txtFromDate.Text = todaydate; 
txtTodate.Text = todaydate; 
} 
if (Request.QueryString["act"] == "Print") 
{ 
BreakageBLL obj = new BreakageBLL(); 
string doclocation = Server.MapPath("~/Pharmacy/cryBreakage.rpt"); 
doc.Load(doclocation); 
string fromDate = Request.QueryString["fromdate"].ToString(); 
string toDate = Request.QueryString["todate"].ToString(); 
//obj.FromDate = Convert.ToDateTime(fromDate); 
// obj.ToDate = Convert.ToDateTime(toDate); 
ds = obj.GetBreakageDetailsforReport(); 
doc.SetDataSource(obj.GetBreakageDetailsforReport()); 
BreakageReportViewer.ReportSource = doc; 
doc.SetParameterValue("nepdate", todaydate); 
doc.SetParameterValue("fdate", fromDate); 
doc.SetParameterValue("tdate", toDate); 
BreakageReportViewer.DataBind(); 
} 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
string fromDate = txtFromDate.Text; 
string toDate = txtTodate.Text; 
//Response.Redirect("breakageReport.aspx?act=print&fromdate=" +fromDate+ "&todate="+ toDate+""); 
Response.Redirect("breakageReport.aspx?act=Print&fromdate=" +fromDate + "&todate=" + toDate + ""); 
} 
//Print Button
protected void btnPrint_Click(object sender, EventArgs e) 
{ 
try 
{ 
if (ds.Tables.Count < 1) 
{ 
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Report is not generated. Please generate report first to print.')", true); 
// doc.PrintToPrinter(1, true, 0, 0); 
} 

else 
{ 
//Print Code
doc.PrintToPrinter(1, true, 0, 0); 
//ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Report is not generated. Please generate report first to print.')", true); 
}
} 
catch (Exception ex) 
{ }
} 
} 
}

You Might Also Like

0 comments :