Monday, November 21, 2005

Crystal Report in a Web application fails to log on to SQL Server

When you use SQL Server 2000 to obtain live data for a Crystal Report in a Visual Studio .NET Web application, the Crystal Report fails to log on to the database.

This is because for security reasons, the SQL Server password is not persisted in the Crystal Report at run time.

To solve this problem, you must deliver the connection information to each database table individually to access the database as a whole. To deliver the connection information, add variables to the code of your Microsoft Visual C# .NET Web Form as follows:

// Add namespaces at top.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

//Crystal Report Variables
CrystalReport1 crReportDocument = new CrystalReport1();

//’CrystalReport1’ must be the name the CrystalReport
TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();

//Crystal Report Properties
CrystalDecisions.CrystalReports.Engine.Database crDatabase;
CrystalDecisions.CrystalReports.Engine.Tables crTables;
CrystalDecisions.CrystalReports.Engine.Table crTable;

//Then, use following code sample to add the logic in the Page_Load method of your Web Form:


crConnectionInfo.ServerName = "EnterServerNameHere";
crConnectionInfo.DatabaseName = "EnterDatabaseNameHere";
crConnectionInfo.UserID = "EnterUserIDHere";
crConnectionInfo.Password = "EnterPasswordHere";
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;

foreach(CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
}

CrystalReportViewer1.ReportSource = crReportDocument;

For detailed info, plaese go to:

http://support.microsoft.com/default.aspx?scid=kb;en;319264&sd=msdn