views:

37

answers:

1

Hi,

Can someone tell me if it's possible to load external CrystalReports (2008) .rpt files? I now embed them so they're compiled with my core.

It would be nice if i can make changes to a reports layout without having to recompile anything. Is that possible?

Thanks.

+1  A: 

Yes, this is possible. In the following example, the .rpt file can be uploaded and then set as the source of a standard ASP.NET CrystalReportViewer control (in this case, reportViewer).

using CrystalDecisions.CrystalReports.Engine;

...

ReportDocument document = new ReportDocument();
document.Load(reportPath); //"C:\\path\\to\\report\\file.rpt";
reportViewer.ReportSource = document;
reportViewer.RefreshReport();

If the database credentials embedded in the .rpt file are incorrect, they can be set as follows:

using CrystalDecisions.Shared;

...

private void ViewReport()
{
 ConnectionInfo connInfo = new ConnectionInfo();
 connInfo.ServerName = "dbservername";
 connInfo.DatabaseName = "dbname";
 connInfo.UserID = "dbusername";
 connInfo.Password = "dbpassword";
 reportViewer.ReportSource = GetReportSource(connInfo);
 reportViewer.RefreshReport();
}

private ReportDocument GetReportSource(ConnectionInfo connInfo)
{
 ReportDocument document = new ReportDocument();
 document.Load(reportPath); //"C:\\path\\to\\report\\file.rpt";
 TableLogOnInfos logonInfos = new TableLogOnInfos();
 TableLogOnInfo logonInfo = new TableLogOnInfo();
 Tables tables;
 tables = document.Database.Tables;
 foreach(CrystalDecisions.CrystalReports.Engine.Table table in tables)
 {
  logonInfo = table.LogOnInfo;
  logonInfo.ConnectionInfo = connInfo;
  table.ApplyLogOnInfo(logonInfo);
 }
 return document;
}
Ryan Shripat
And my ReportDataSource is a DataSet from the core DLL.I can just set it as i used to?"rpt.SetDataSource(ds);"
Jeroen
I believe so, but I haven't used Crystal Reports with a DataSet backend.
Ryan Shripat
It should be noted that `ReportDocument.Load()` expects the physical file path. So, use `Server.MapPath()` in an asp.net context.
dotjoe