public ReportDocument LoadCrystalReportViewer(CrystalReportInfo crInfo, out string message) { message = ""; try { if (crInfo != null) { string ReportPath = null; ReportDocument rptDocument = new ReportDocument(); if (crInfo.IsFullPath) { ReportPath = crInfo.ReportName; } else { ReportPath = Application.StartupPath + "/Reports/" + crInfo.ReportName + ".rpt"; } if (!System.IO.File.Exists(ReportPath)) { message = "Report not found."; return null; } rptDocument.Load(ReportPath); //Set Value to Formulas if exists if (crInfo.FormulaList != null) { foreach (var f in crInfo.FormulaList) { if (f.SubReportName == null || f.SubReportName.Trim() == "") { rptDocument.DataDefinition.FormulaFields[f.FormulaName].Text = "\"" + f.FormulaValue + "\""; } else { rptDocument.Subreports[f.SubReportName].DataDefinition.FormulaFields[f.FormulaName].Text = "\"" + f.FormulaValue + "\""; } } } if (crInfo.IsConnectDirectSQL == false) { ///Set Database to report /// if (crInfo.DataSourceList != null) { foreach (var o in crInfo.DataSourceList) { if (o.SubReportName == null || o.SubReportName.Trim() == "") { rptDocument.Database.Tables[o.TableName].SetDataSource(o.DataSource); //rptDocument.SetDataSource(o.DataSource); } else { rptDocument.Subreports[o.SubReportName].Database.Tables[o.TableName].SetDataSource(o.DataSource); } } } } else { //Set connection info to report login to database sql, oracle, ... & set parameters ConnectionInfo LogonInfo = new ConnectionInfo(); LogonInfo.ServerName = crInfo.ServerName; LogonInfo.UserID = crInfo.UserName; LogonInfo.Password = crInfo.Password; LogonInfo.DatabaseName = crInfo.DatabaseName; TableLogOnInfo tableLogOnInfo = new TableLogOnInfo(); foreach (Table table in rptDocument.Database.Tables) { tableLogOnInfo = table.LogOnInfo; tableLogOnInfo.ConnectionInfo = LogonInfo; table.ApplyLogOnInfo(tableLogOnInfo); } ////Apply log on Info to Subreport foreach (ReportDocument srd in rptDocument.Subreports) { foreach (CrystalDecisions.CrystalReports.Engine.Table table in srd.Database.Tables) { tableLogOnInfo = table.LogOnInfo; tableLogOnInfo.ConnectionInfo = LogonInfo; table.ApplyLogOnInfo(tableLogOnInfo); } } //Set Params if (crInfo.ParamList != null) { foreach (var p in crInfo.ParamList) { if (p.SubReportName == null || p.SubReportName.Trim() == "") { rptDocument.SetParameterValue(p.ParamName, p.ParamValue); } else { rptDocument.SetParameterValue(p.ParamName, p.ParamValue, p.SubReportName); } } } } //Show Preview if (crInfo.IsPreview) { crystalReportViewer1.ReportSource = rptDocument; } else { //Print to Printer if (!string.IsNullOrEmpty(crInfo.PrinterName)) { rptDocument.PrintOptions.PrinterName = crInfo.PrinterName; } rptDocument.PrintToPrinter(1, false, 0, 0); } return rptDocument; } else { message = "Please initial information before load report."; return null; } } catch (Exception ex) { message = ex.Message; return null; } }
RptDocument.SetDatabaseLogon(crInfo.UserName, crInfo.Password, crInfo.ServerName, crInfo.DataBaseName);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)