views:

359

answers:

3

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.

A: 

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
Arvo
Thanks Arvo - looks like a more complete version of what I posted at the same time.
LordSauce
Yeh, but needs refactored. Legacy VB6 code, youknow...One what I have to say - CR object model compatibility over versions seems pretty good. My code is many versions old and is working so far, although newer methods appear to be more friendly.
Arvo
A: 

Bit of experimentation seems to solved the problem:

    private void Form1_Load(object sender, EventArgs e)
    {
        ReportDocument rd = new ReportDocument();
        rd.Load("Report.rpt");

        Explore(rd);

        foreach (ReportDocument sr in rd.Subreports)
        {
            Explore(sr);
        }
    }

    private void Explore(ReportDocument r)
    {
        foreach (IConnectionInfo con in r.DataSourceConnections)
        {
            if (!r.IsSubreport)
                Console.WriteLine("Main Report");
            else
                Console.WriteLine(r.Name);
            Console.WriteLine(con.DatabaseName);
            Console.WriteLine("-");
        }
    }
LordSauce
+3  A: 

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

Ben