Search code examples
crystal-reports

Crystal Reports Configuration Tool


I have a Crystal report with 50 odd subreports, each with loads of parameters. Switching it from one database to another takes ages as the Crystal Reports IDE insists that you enter all the parameters for each sub-report.

I'm wondering if it's possible to write a quick tool in C# to view the current database config of all of the sub-reports in an rpt file, and ideally to switch to a different database.

Unfortunately (or fortunately) I don't have much experience of the Crystal object model - anyone know where to start?

Thanks, Jon.


Solution

  • This should do the job. Obviously replace the passwords and User names where neccesary.

    Private Sub ProcessFile(ByVal FileName As String)
            Dim CR As Engine.ReportDocument = Nothing
            Try
                CR = New Engine.ReportDocument
                CR.Load(FileName, CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
    
                'Recurse thru Report
                RecurseAndRemap(CR)
                'Save File
                CR.SaveAs("OutPutFilePath")
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                If Not CR Is Nothing Then
                    CR.Close()
                    CR.Dispose()
                End If
            End Try
        End Sub
    
        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
    

    Hope that helps Cheers Ben