Crystal Reports Customization Tool

I have a Crystal report with 50 odd nested reports, each with a lot of parameters. Switching it from one database to another requires age, since the ID Crystal Reports environment insists that you enter all the parameters for each sub-report.

I am wondering if it is possible to write a quick tool in C # to view the current database configuration of all subtasks in the rpt file and, ideally, switch to another database.

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

Thanks, John.

+4
source share
3 answers

That should do the job. Obviously, replace passwords and usernames where necessary.

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

+7
source

In VB6, we use something like the following (dirty copy-paste, 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 
+1
source

A little experimentation seems to solve 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("-"); } } 
0
source

All Articles