2
votes

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...

2

2 Answers

0
votes
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
0
votes

I'm not sure if the web viewer is the same as the winforms viewer, but what I ended up having to do was create a new ReportDocument, load the report file into the ReportDocument, and then change all of the connection information.

ReportDocument currentReport = new ReportDocument();  
currentReport.Load([path to .rept], OpenReportMethod.OpenReportByTempCopy);
ConnectionInfo crConnectionInfo = new ConnectionInfo();

//Set your connection params here  

for (int i = 0; i < currentReport.Database.Tables.Count; i++)
{
    Table crTable = currentReport.Database.Tables[i];
    TableLogOnInfo crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo);
}

foreach(IConnectionInfo cn in currentReport.DataSourceConnections)
{
    cn.SetConnection(_server, _database, _windowsAuth);        
    cn.SetLogon(_userName, _password);
}  

Then just set your report viewer source to the report object:

CrystalReportViewer.ReportSource = currentReport;