Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crystal Reports Configuration Tool [closed]

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.

like image 316
Jonathan Sayce Avatar asked Feb 28 '23 19:02

Jonathan Sayce


2 Answers

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

like image 100
Ben Avatar answered Mar 03 '23 11:03

Ben


In VB6 we use something like next (dirty copy-paste form old code, incrementally updated from CR6 to CR9), maybe you can get some ideas:

For Each tmpTable In Report.Database.Tables
  Set CPProperties = tmpTable.ConnectionProperties
  CPProperties.DeleteAll
  CPProperties.Add "Provider", "SQLOLEDB"
  CPProperties.Add "Data Source", mServerName
  CPProperties.Add "Initial Catalog", mBaseName
  CPProperties.Add "User ID", mUserID
  CPProperties.Add "Password", mPassword
  CPProperties.Add "Server Name", mServerName
  CPProperties.Add "Server Type", "OLEDB"
  CPProperties.Add "DataBase", mBaseName
  tmpTable.SetTableLocation tmpTable.Location, "", ""
Next tmpTable
For Each tmpSection In Report.Sections
    For Each tmpObject In tmpSection.ReportObjects
        If TypeName(tmpObject) = "ISubreportObject" Then
            Set tmpReport = tmpObject.OpenSubreport()
            For Each tmpTable In tmpReport.Database.Tables
              Set CPProperties = tmpTable.ConnectionProperties
              CPProperties.DeleteAll
              CPProperties.Add "Provider", "SQLOLEDB"
              CPProperties.Add "Data Source", mServerName
              CPProperties.Add "Initial Catalog", mBaseName
              CPProperties.Add "User ID", mUserID
              CPProperties.Add "Password", mPassword
              CPProperties.Add "Server Name", mServerName
              CPProperties.Add "Server Type", "OLEDB"
              CPProperties.Add "DataBase", mBaseName
              tmpTable.SetTableLocation tmpTable.Location, "", ""
            Next tmpTable
        End If
    Next tmpObject
Next tmpSection
like image 20
Arvo Avatar answered Mar 03 '23 10:03

Arvo