C# – Change Crystal Reports Viewer Connection String ASP.NET

asp.netccrystal-reports

In Visual Studio 2010, I am dynamically populating a list of Crystal Reports based on an XML file that have the following settings:

<Report file="C:\reportname.rpt"    text="Report Name"
       imageURL="~/images/reporticon.png" />

In my ASPX page I have a CrystalReportsViewer like the following:

<CR:CrystalReportViewer ID="CrystalReportViewer" runat="server" AutoDataBind="true"
    Width="800px" Height="600px" CssClass="reportViewer"   HasCrystalLogo="False" />

When a user clicks on a report link (Comes from a TreeNode object), the crystal report viewer report is set like the following:

 CrystalReportViewer.ReportSource = "C:\reportname.rpt";

In my actual RPT report files, I have connection strings saved in them so the Crystal Report Viewer does not prompt the user to enter a user name and password.

My question is to find out if it is possible to change the connection string that is saved in the report file? How can I set the connection string information when I load an RPT file into the Crystal Reports Viewer?

Thanks in advance for any help…

Best Answer

Private Sub RecurseAndRemap(ByVal CR As Engine.ReportDocument)
        For Each DSC As CrystalDecisions.Shared.IConnectionInfo In CR.DataSourceConnections
            DSC.SetLogon("YourUserName", "YourPassword")
            DSC.SetConnection("YouServerName", "YourDatabaseName", False)
        Next

        CR.SetDatabaseLogon("YourUserName", "YourPassword")

        For Each Table As Engine.Table In CR.Database.Tables
            Table.LogOnInfo.ConnectionInfo.UserID = "YourUserName"
            Table.LogOnInfo.ConnectionInfo.Password = "YourPassword"
        Next

        If Not CR.IsSubreport Then
            For Each SR As Engine.ReportDocument In CR.Subreports
                RecurseAndRemap(SR)
            Next
        End If
    End Sub
Related Topic