I am generating a report, but the problem is even though I\'ve supplied credentials, when the form containing the CrystalReport opens up, it still asks me for them, and the
We have lots of problems when we came to connect a crystal report to a different database to the one it was designed / created against. We ended up creating the following function to set it up correctly. It recursively sets the connection on all report tables and sub reports.
Also I seem to remember we had problems if the report was designed with Integrated Windows Authentications, and run with a simple username and password. So we always made sure we designed the reports with a simple username and password connection to the database.
private static void SetConnection(ReportDocument report, string databaseName, string serverName, string userName, string password)
{
foreach (Table table in report.Database.Tables)
{
if (table.Name != "Command")
{
SetTableConnectionInfo(table, databaseName, serverName, userName, password);
}
}
foreach (ReportObject obj in report.ReportDefinition.ReportObjects)
{
if (obj.Kind != ReportObjectKind.SubreportObject)
{
return;
}
var subReport = (SubreportObject)obj;
var subReportDocument = report.OpenSubreport(subReport.SubreportName);
SetConnection(subReportDocument, databaseName, serverName, userName, password);
}
}
private static void SetTableConnectionInfo(Table table, string databaseName, string serverName, string userName, string password)
{
// Get the ConnectionInfo Object.
var logOnInfo = table.LogOnInfo;
var connectionInfo = logOnInfo.ConnectionInfo;
// Set the Connection parameters.
connectionInfo.DatabaseName = databaseName;
connectionInfo.ServerName = serverName;
connectionInfo.Password = password;
connectionInfo.UserID = userName;
table.ApplyLogOnInfo(logOnInfo);
if (!table.TestConnectivity())
{
throw new ApplicationException(Resource.UnableToConnectCrystalReportToDatabase);
}
table.Location = Database + "." + "dbo" + "." + table.Location;
}