I think I see what the problem is!
You're setting the logins for the report in the viewer, NOT the report object that you're exporting. When you assign the report object to the ReportSource property of the viewer, the viewer has a copy of the report and loses its links to the report object. So, if the user has set any parameter values for the report in the viewer, those parameters have not been set for the report object.
Here's code for setting the logins at the report object level before you assign it to the ReportSource:
CrystalDecisions.Shared.ConnectionInfo connectionInfo =
new CrystalDecisions.Shared.ConnectionInfo();
connectionInfo.ServerName = "server";
connectionInfo.DatabaseName = "DB"
connectionInfo.UserID = "user";
connectionInfo.Password = "PW";
// set report connection for main report
SetDBLogonForReport(connectionInfo, crReport, qServer);
// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport, qServer);
private void SetDBLogonForReport
(CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument, string qServer)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
private void SetDBLogonForSubreports
(CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument, string qServer)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument =
subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument, qServer);
}
}
}
}
Also, you could try changing the button click code to do something like this:
MemoryStream oStream; // using System.IO
oStream = (MemoryStream) ((ReportDocument)
crystalReportViewer1.ReportSource.ExportToStream(
ExportFormatType.PortableDocFormat));
Good luck!
-Dell