Ensure you have added references to Crystal Reports as follows;
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.ReportSource
CrystalDecisions.Shared
CrystalDecisions.Windows.Forms
Add the following using statements;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
The below code is used to open a Crystal Report & apply the Logon information.
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load("<Path & file name of the report>");
ConnectionInfo conRpt = new ConnectionInfor();
conRpt.ServerName = "DB Server Name";
conRpt.DatabaseName = "DB Name";
conRpt.UserID = "User name";
conRpt.Password = "Password";
Tables rptTables = rptDoc.Database.Tables;
for(int i = 0; i < rptTables.Count; i++)
{
Table rptTable = rptTables[i];
TableLogOnInfo tblInfo = rptTable.LogOnInfo;
tblInfo.ConnectionInfo = conRpt;
}
if(rptDoc.SubReports.Count > 0)
{
for(int i = 0; i < rptDoc.Subreports.Count; i++)
{
using(ReportDocument rptSub = rptDoc.OpenSubReport(rptDoc.SubReports[i].Name))
{
Tables rptTables = rptSub.Database.Tables;
for(int i = 0; i < rptTables.Count; i++)
{
Table rptTable = rptTables[i];
TableLogOnInfo tblInfo = rptTable.LogOnInfo;
tblInfo.ConnectionInfo = conRpt;
}
rptSub.Close();
}
}
}
frmReportViewer.ReportSource = rptDoc;
Kind Regards