private static void AssignConnectionToTables(ConnectionInfo connection, CrystalDecisions.CrystalReports.Engine.Database reportDatabase) { // First we assign the connection to all tables in the main report // foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportDatabase.Tables) { // Cache the logon info block TableLogOnInfo logOnInfo = table.LogOnInfo; // Set the connection logOnInfo.ConnectionInfo = connection; // Apply the connection to the table! table.ApplyLogOnInfo(logOnInfo); } }
public byte[] GenerateCrystalReport() { string reportPath = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "CReports\\Reports"); string reportImagePath = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "CReports\\Images"); //string _ReportImagePath = ""; //string _ReportPath = ""; string path = reportPath; //_ReportImagePath = reportImagePath; if (String.IsNullOrEmpty(path)) { throw new MissingFieldException("CrystalReportPathNotSet"); } if (reportName.IndexOf(":") < 0) { reportPath = path + "\\" + reportName; } else { reportPath = reportName; } ReportDocument rptBurndown = new ReportDocument(); if (File.Exists(reportPath)) //Check if the crystal report file exists in a specified location. { try { rptBurndown.Load(reportPath); //Set Connection Info ConnectionInfo.Get().SetAttributes(System.Configuration.ConfigurationManager.AppSettings["oracleConnectionString"]); //Application will pick database info from the property file. CrystalDecisions.Shared.ConnectionInfo crDbConnection = new CrystalDecisions.Shared.ConnectionInfo(); crDbConnection.IntegratedSecurity = false; crDbConnection.DatabaseName = ConnectionInfo.Get().Db_name; crDbConnection.UserID = ConnectionInfo.Get().Db_uid; crDbConnection.Password = ConnectionInfo.Get().Db_pwd; //crDbConnection.Type = ConnectionInfoType.Unknown; crDbConnection.ServerName = ConnectionInfo.Get().Db_host; CrystalDecisions.CrystalReports.Engine.Database crDatabase = rptBurndown.Database; CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo; foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in crDatabase.Tables) { crDbConnection.IntegratedSecurity = false; crDbConnection.DatabaseName = ConnectionInfo.Get().Db_name; crDbConnection.UserID = ConnectionInfo.Get().Db_uid; crDbConnection.Password = ConnectionInfo.Get().Db_pwd; //crDbConnection.Type = ConnectionInfoType.Unknown; crDbConnection.ServerName = ConnectionInfo.Get().Db_host; oCrTableLoginInfo = oCrTable.LogOnInfo; oCrTableLoginInfo.ConnectionInfo = crDbConnection; oCrTable.ApplyLogOnInfo(oCrTableLoginInfo); } //Create Parameter query string sql = SqlQuery; StringBuilder sb = new StringBuilder(" WHERE "); if (_pi.GetRecord_ID() > 0 && _pi.GetTable_ID() > 0) { string tableName = DB.ExecuteScalar("SELECT TableName FROM AD_Table WHERE AD_TABLE_ID =" + _pi.GetTable_ID()).ToString(); sb.Append(tableName).Append("_ID = ").Append(_pi.GetRecord_ID()); } else { ProcessInfoUtil.SetParameterFromDB(_pi); ProcessInfoParameter[] parameters = _pi.GetParameter(); if (parameters.Count() > 0) { int loopCount = 0; for (int para = 0; para <= parameters.Count() - 1; para++) { string sInfo = parameters[para].GetInfo(); string sInfoTo = parameters[para].GetInfo_To(); if ((String.IsNullOrEmpty(sInfo) && String.IsNullOrEmpty(sInfoTo)) || sInfo == "NULL") { continue; } if (loopCount > 0) { sb.Append(" AND "); } string paramName = parameters[para].GetParameterName(); object paramValue = parameters[para].GetParameter(); object paramValueTo = parameters[para].GetParameter_To(); if (paramValue is DateTime) { sb.Append(paramName).Append(" BETWEEN ").Append(GlobalVariable.TO_DATE((DateTime)paramValue, true)); if (paramValueTo != null && paramValueTo.ToString() != String.Empty) { sb.Append(" AND ").Append(GlobalVariable.TO_DATE(((DateTime)paramValueTo).AddDays(1), true)); } else { sb.Append(" AND ").Append(GlobalVariable.TO_DATE(((DateTime)paramValue).AddDays(1), true)); } } else if (paramValue != null && paramValue.ToString().Contains(',')) { sb.Append(paramName).Append(" IN (") .Append(paramValue.ToString()).Append(")"); } else { sb.Append("Upper(").Append(paramName).Append(")").Append(" = Upper(") .Append(GlobalVariable.TO_STRING(paramValue.ToString()) + ")"); } loopCount++; } } } if (sb.Length > 7) { sql = sql + sb.ToString(); } //if (form.IsIncludeProcedure()) //{ // bool result = StartDBProcess(form.GetProcedureName(), parameters); //} DataSet ds = DB.ExecuteDataset(sql); if (ds == null) { ValueNamePair error = VLogger.RetrieveError(); throw new Exception(error.GetValue() + "BlankReportWillOpen"); } bool imageError = false; if (isIncludeImage) { for (int i_img = 0; i_img <= ds.Tables[0].Rows.Count - 1; i_img++) { String ImagePath = ""; String ImageField = ""; if (ds.Tables[0].Rows[i_img][imagePathField] != null) { ImagePath = ds.Tables[0].Rows[i_img][imagePathField].ToString(); ImageField = imageField; if (ds.Tables[0].Columns.Contains(ImageField)) { if (File.Exists(reportImagePath + "\\" + ImagePath)) { byte[] b = StreamFile(reportImagePath + "\\" + ImagePath); ds.Tables[0].Rows[i_img][ImageField] = b; } else { //ds.Tables[0].Rows.RemoveAt(i_img); imageError = true; } } else { imageError = true; } } else { imageError = true; } } } if (imageError) { // ShowMessage.Error("ErrorLoadingSomeImages", true); } //crystalReportViewer1.ReportSource = rptBurndown; //crystalReportViewer1.Refresh(); System.IO.Stream oStream; byte[] byteArray = null; rptBurndown.SetDataSource(ds.Tables[0]); //By karan approveed by lokesh...... //rptBurndown.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(100, 360, 100, 360)); oStream = rptBurndown.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); byteArray = new byte[oStream.Length]; oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length)); return(byteArray); //if (form.IsDirectPrint()) //{ // // rptBurndown.PrintOptions.PrinterName = Env.GetCtx().GetPrinterName(); // //rptBurndown.PrintToPrinter(1, false, 0, 0); //} } catch (Exception ex) { throw ex; } } else { throw new MissingFieldException("CouldNotFindTheCrystalReport"); } }