PageRenderTime 20ms CodeModel.GetById 6ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Reports/ViewReport.aspx.cs

http://github.com/khaneh/Orders
C# | 164 lines | 115 code | 24 blank | 25 comment | 10 complexity | bdbe06563ca6b62e63630aabc620000b 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 GeneralReportWebForm.
 11	/// </summary>
 12	public class GeneralReportWebForm : System.Web.UI.Page
 13	{
 14		protected System.Web.UI.WebControls.Button Button1;
 15		protected System.Web.UI.HtmlControls.HtmlForm Form1;
 16		protected CrystalDecisions.Web.CrystalReportViewer viewer;
 17		protected GeneralReport report = null;
 18		protected ParameterFields parameterFields = new ParameterFields ();
 19
 20		protected int reportLogID=0; 
 21		protected int totalPageCount=0;
 22		protected string errors="";
 23	
 24		private void Page_Load(object sender, System.EventArgs e)
 25		{
 26			char failed='0';
 27			try
 28			{
 29				reportLogID = Int32.Parse(Request.QueryString.ToString());
 30			}
 31			catch 
 32			{
 33				errors+="Invalid ReportNumber\n<br>";
 34			}
 35
 36			string[] strParameterNames=new string[] {};
 37			string[] strParameterValues=new string[] {};
 38			
 39			//SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=sefareshat");
 40			SqlConnection conn = new SqlConnection("data source=localhost;initial catalog=sefareshat;password=5tgb;persist security info=True;user id=sefadmin;workstation id=appserv;packet size=4096");
 41
 42			conn.Open();
 43
 44			SqlCommand sqlCommand = new SqlCommand();
 45			sqlCommand.Connection=conn;
 46
 47			sqlCommand.CommandText="SELECT ReportFileName,ReportParameterNames,ReportParameterValues FROM ReportLog WHERE ID='"+ reportLogID +"' AND Checked=0";
 48
 49			SqlDataReader dr = sqlCommand.ExecuteReader();
 50
 51			if (dr.Read())
 52			{
 53				report=new GeneralReport(dr.GetString(0));
 54				string tmpParameterNames=dr.GetString(1);
 55				string tmpParameterValues=dr.GetString(2);
 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			for (int i=0; i<strParameterNames.Length; i++)
 68			{
 69				ParameterDiscreteValue discreteVal = new ParameterDiscreteValue ();
 70				ParameterField paramField = new ParameterField ();
 71
 72				paramField.ParameterFieldName = strParameterNames[i];
 73				discreteVal.Value = strParameterValues[i];
 74				paramField.CurrentValues.Add (discreteVal);
 75				parameterFields.Add (paramField);
 76			}
 77			if(! initViewer())
 78				errors+="Cannot Initialize Viewer.\n<br>";
 79			
 80			if (errors!="")
 81			{
 82				failed='1';
 83				Response.Write(errors);
 84			}
 85
 86			if(errors.Length>180)
 87				errors = errors.Substring(0,180);
 88
 89			sqlCommand.CommandText="UPDATE ReportLog Set Checked='1',Viewed='1',Failed='"+failed+"',ErrorDescription=N'"+ Util.sqlSafe(errors) +"',PrinterName='Not Printed' WHERE (ID='"+ reportLogID +"')";
 90			sqlCommand.ExecuteNonQuery();
 91
 92			conn.Close();
 93//			if (report!=null) report.Dispose();
 94			if (report!=null)
 95			{
 96				// Response.Write("###+(" + totalPageCount + ") ###");
 97				if (totalPageCount<2)
 98					report.Dispose();
 99			}
100
101			if (errors!="")
102				Response.End();
103		}
104
105		private bool initViewer()
106		{
107			bool success=true;
108			try
109			{
110				viewer.ReportSource = report;
111				for (int i=0; i<report.DataDefinition.ParameterFields.Count; i++)
112				{
113					report.DataDefinition.ParameterFields[i].ApplyCurrentValues(parameterFields[i].CurrentValues);
114				}
115				viewer.RefreshReport();
116
117				CrystalDecisions.Shared.ReportPageRequestContext reqContext = (CrystalDecisions.Shared.ReportPageRequestContext) viewer.RequestContext;
118
119				totalPageCount = report.FormatEngine.GetLastPageNumber(reqContext); 
120
121				/*
122				*	Another way wich also works:
123				* 
124					for (int i=0; i<report.DataDefinition.ParameterFields.Count; i++)
125					{
126						report.DataDefinition.ParameterFields[i].CurrentValues.Add (parameterDiscreteValues[i]); 
127					}
128					CrystalReportViewer1.ReportSource = (ReportClass) report;
129					CrystalReportViewer1.ParameterFieldInfo = parameterFields;
130					CrystalReportViewer1.RefreshReport();
131				*/
132			}
133			catch (Exception e)
134			{
135//				errors += e.ToString()+"\n<br><br>";
136				success=false;
137			}
138			return success;
139		}
140
141		#region Web Form Designer generated code
142		override protected void OnInit(EventArgs e)
143		{
144			//
145			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
146			//
147			InitializeComponent();
148			base.OnInit(e);
149			DataBind();
150		}
151		
152		/// <summary>
153		/// Required method for Designer support - do not modify
154		/// the contents of this method with the code editor.
155		/// </summary>
156		private void InitializeComponent()
157		{    
158			this.Load += new System.EventHandler(this.Page_Load);
159
160		}
161		#endregion
162
163	}
164}