1197326 - How to change the data source location of a Crystal report at design time using Visual Studio .NET

SAP Knowledge Base Article - Public

1197326 - How to change the data source location of a Crystal report at design time using Visual Studio .NET

Symptom

You may want to change the data source location of one or many reports at design time. For example, you may have recently changed the location of your database, and you need to run a batch program that will alter the Crystal Reports data source location.

Assume that you have a report called "c:\reports\test.rpt". This report has a data source location named Northwind eal (please refer to the See Also section of this Note for more details about the data sources).

Now you want to change the data source location for one or many reports at the push of a button. The new data source has exactly the same structure as the previous data source - only its location is now different (as set in the ODBC Data Sources).

Resolution

In Visual Studio .NET, use a ReportDocument object to load a RPT file, change the data source location then export the ReportDocument to a RPT file on the hard drive to persist the changes.

Follow these steps:

1. In Windows, on the Start menu, click Run, then type "odbcad32". Click OK. The ODBC Data Source Administrator dialog box appears.

2. Create two System DSNs; one named Northwind eal and the other named Northwind van (refer to the See Also section).

3. Create a new Crystal report with its fields based on the Northwind eal DSN. Save this report as C:\reports\test.rpt.

4. Create a new Visual Studio project.

5. In Visual Studio, add CrystalDecisions.CrystalReports.Engine and CrystalDecisions.Shared as references.

6. Add a new form with a button. After adding the button to the form, double-click the button to set up the Button1_Click procedure. Add the following code (VB or C#) to the Button1_Click procedure:

VB

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rep As ReportDocument = New ReportDocument
Dim path As String
Dim done As Boolean

'specify path to the report
path = "c:\reports\test.rpt"

'load the report
rep.Load(path)

'use Logon method from c2011464
done = Logon(rep, "Northwind van", "Northwind", "<username>", "<password>")
rep.ExportToDisk(ExportFormatType.CrystalReport, "c:\reports\test_export.rpt")
rep.Refresh()
rep.Close()

End Sub

C#

private void Button1_Click(object sender, System.EventArgs e)
{
ReportDocument rep = new ReportDocument();
//specify path to the report
string path = "c:\\reports\\test.rpt";

//load the report
rep.Load(path);

//use Logon method from c2011464
done = Logon(rep, "Northwind van", "Northwind", "<username>", "<password>");
rep.ExportToDisk(ExportFormatType.CrystalReport, "c:\\reports\\test_export.rpt");
rep.Close();

}

7. Now you need to add code to change the data source. Add the code from Note 1214777 directly after the Button1_Click procedure.

8. Create an output directory on your test computer called C:\reports.

9. Build and run the solution.

10. The new report will save as C:\reports\test_export.rpt.

11. Open this new report, and refresh it once (you may have to log on to the database again). It will contain the new data source location as was specified in the code.

NOTE
  • This work flow has been tested using Crystal Reports .NET 2003 and Crystal Reports XI Release1.
  • The sample database used in this case was the Northwind sample database that can be installed with SQL Server.

See Also

ODBC DSNs used for this Note


Type: ODBC (RDO)
Data Source Name: Northwind eal
User ID: sa
Database: Northwind

Type: ODBC (RDO)
Data Source Name: Northwind van
User ID: sa
Database: Northwind

Keywords

.Net data source location odbc , 2754396 , KBA , BI-RA-CR , Crystal Reports designer or BusinessViews Manager , How To

Product

SAP Crystal Reports XI ; SAP Crystal Reports XI R2 ; SAP Crystal Reports, version for Visual Studio .NET 2005 ; SAP Crystal Reports, version for Visual Studio .NET 2008 ; SAP Crystal Reports, version for Visual Studio .NET 9.1