Microsoft KB Archive/319264

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

Article ID: 319264

Article Last Modified on 1/2/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft SQL Server 2000 Service Pack 3a
 * Microsoft SQL Server 2000 Service Pack 2
 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Express Edition
 * Microsoft SQL Server 2005 Workgroup Edition

-



This article was previously published under Q319264



SYMPTOMS
When you use Microsoft SQL Server 2000 or SQL Server 2005 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 occurs even if you provide accurate credentials at design time. You receive the following error message in your Web browser:

Server Error in '/Crystal' Application.

---

Logon failed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.LogOnException: Logon failed.



CAUSE
For security reasons, the SQL Server password is not persisted in the Crystal Report at run time.



RESOLUTION
To resolve 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 = &quot;EnterServerNameHere&quot;; crConnectionInfo.DatabaseName = &quot;EnterDatabaseNameHere&quot;; crConnectionInfo.UserID = &quot;EnterUserIDHere&quot;; crConnectionInfo.Password = &quot;EnterPasswordHere&quot;; 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 the preceding code to run successfully, you must type your ServerName, DatabaseName, UserID, and Password. The Crystal Report in this example (CrystalReport1.rpt) is set up by using OLE DB and SQL Server authentication.

For more detailed information about how to create a Web application that uses the Crystal Reports viewer (CRViewer), see the &quot;Steps to Reproduce the Behavior&quot; portion of the &quot;More Information&quot; section.

Note Crystal Decisions handles all support questions and support issues concerning Crystal Reports for Visual Studio .NET, except installation questions. See the &quot;References&quot; section of this article for procedures to follow for help with support questions and support issues.



STATUS
This behavior is by design.



MORE INFORMATION
Note that this problem does not occur when you use a Microsoft Visual C# .NET or a Microsoft Visual Basic .NET Windows application. To reproduce this problem, you must use a Web application.

Steps to reproduce the behavior
 In Microsoft Visual Studio .NET, create a new Microsoft ASP.NET Web application in Microsoft Visual C# .NET. On the File menu, click Add New Item, and then click Add a Crystal Report. Click Using the Report Expert in the dialog box to create your report. Follow the steps in the wizard to set up the connection to your SQL Server database. From the Toolbox, double-click CrystalReportsViewer to add this control to WebForm1.aspx. Right-click WebForm1.aspx in the Solution Explorer window, and then click View Code.</li>  In the Page_Load method, add the following code: CrystalReportViewer1.ReportSource = &quot;C:\\Inetpub\\wwwroot\\...\\CrystalReport1.rpt&quot;; where C:\\Inetpub\\wwwroot\\...\\CrystalReport1.rpt is the path to your local copy of the Crystal Report.

</li> Verify that the ASPNET account has Read and Write access to your Crystal Report.</li> Compile and run your application. Notice that you receive the error message that is listed in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">