PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Reports/PrintReport.aspx.cs

http://github.com/khaneh/Orders
C# | 146 lines | 115 code | 20 blank | 11 comment | 9 complexity | 0abae52d6f84e7fbd827c8a9aa4429e1 MD5 | raw file
  1using System;
  2using System.Data.SqlClient;
  3using CrystalDecisions.CrystalReports.Engine;
  4using CrystalDecisions.Shared;
  5using CrystalDecisions.Web;
  6
  7namespace Reports
  8{
  9	/// <summary>
 10	/// Summary description for PrintReport.
 11	/// </summary>
 12	public class PrintReport : System.Web.UI.Page
 13	{
 14		protected GeneralReport report = null;
 15		protected ParameterFields parameterFields = new ParameterFields ();
 16
 17		protected int reportLogID=0; 
 18
 19		private void Page_Load(object sender, System.EventArgs e)
 20		{
 21			string errors="";
 22			string returnURL="";
 23			string reportName="";
 24			char failed='0';
 25
 26			try
 27			{
 28				reportLogID = Int32.Parse(Request.QueryString.ToString());
 29			}
 30			catch 
 31			{
 32				errors+="Invalid ReportNumber\n<br>";
 33			}
 34
 35			string[] strParameterNames=new string[] {};
 36			string[] strParameterValues=new string[] {};
 37			
 38			//SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=sefareshat");
 39			SqlConnection conn = new SqlConnection("data source=appserv;initial catalog=sefareshat;password=5tgb;persist security info=True;user id=sefadmin;workstation id=appserv;packet size=4096");
 40			conn.Open();
 41
 42			SqlCommand sqlCommand = new SqlCommand();
 43			sqlCommand.Connection=conn;
 44
 45			sqlCommand.CommandText="SELECT ReportFileName,ReportParameterNames,ReportParameterValues,ReturnURL FROM ReportLog WHERE ID='"+ reportLogID +"' AND Checked=0";
 46		
 47			SqlDataReader dr = sqlCommand.ExecuteReader();
 48
 49			if (dr.Read())
 50			{
 51				reportName=dr.GetString(0);
 52				report=new GeneralReport(reportName);
 53				string tmpParameterNames=dr.GetString(1);
 54				string tmpParameterValues=dr.GetString(2);
 55				returnURL=dr.GetString(3);
 56				if(tmpParameterNames!="")
 57				{
 58					strParameterNames=tmpParameterNames.Split((char)1);
 59					strParameterValues=tmpParameterValues.Split((char)1);
 60				}
 61			}
 62			else
 63			{
 64				errors+="No such Report\n<br>";
 65			}
 66			dr.Close();
 67			try
 68			{
 69				for (int i=0; i<strParameterNames.Length; i++)
 70				{
 71					ParameterDiscreteValue discreteVal = new ParameterDiscreteValue ();
 72					ParameterField paramField = new ParameterField ();
 73
 74					paramField.ParameterFieldName = strParameterNames[i];
 75					discreteVal.Value = strParameterValues[i];
 76					paramField.CurrentValues.Add (discreteVal);
 77					parameterFields.Add (paramField);
 78				}
 79			}
 80			catch
 81			{
 82				errors+="Invalid Report Parameters...\n<br>";
 83			}
 84			
 85			string printerName=Util.choosePrinter(Request.UserHostAddress,reportName);
 86			if(! Print(printerName))
 87				errors+="Cannot Print Report...\n<br>";
 88
 89			if (errors!="")
 90			{
 91				failed='1';
 92				Response.Write(errors);
 93			}
 94			
 95			sqlCommand.CommandText="UPDATE ReportLog Set Checked='1',Printed='1',Failed='"+failed+"',ErrorDescription=N'"+ Util.sqlSafe(errors) +"',PrinterName='"+ Util.sqlSafe(printerName)+"' WHERE (ID='"+ reportLogID +"') AND (Printed=0) ";
 96			sqlCommand.ExecuteNonQuery();
 97
 98			conn.Close();
 99			if (report!=null) report.Dispose();
100			if (returnURL!="" && errors=="")
101				Response.Redirect(returnURL,true);
102			Response.End();
103		}
104
105		private bool Print(string PrinterName)
106		{
107			bool success=true;
108
109			try
110			{
111				for (int i=0; i<report.DataDefinition.ParameterFields.Count; i++)
112				{
113					report.DataDefinition.ParameterFields[i].ApplyCurrentValues(parameterFields[i].CurrentValues);
114				}
115				report.PrintOptions.PrinterName = PrinterName;
116				report.PrintToPrinter(1,false,1,99);
117			}
118			catch
119			{
120				success=false;
121			}
122			return success;
123		}
124
125
126		#region Web Form Designer generated code
127		override protected void OnInit(System.EventArgs e)
128		{
129			//
130			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
131			//
132			InitializeComponent();
133			base.OnInit(e);
134		}
135		
136		/// <summary>
137		/// Required method for Designer support - do not modify
138		/// the contents of this method with the code editor.
139		/// </summary>
140		private void InitializeComponent()
141		{    
142			this.Load += new System.EventHandler(this.Page_Load);
143		}
144		#endregion
145	}
146}