I know how to change the database and sqlserver for a crystal report
Now in the application the report must be able to change database tables according to the year that we are in. for instance i have a table VS2009 and VS2010 and i want the data from the VS2009 table instead of the VS2010 data. how do i change the table for the report. Both tables are identical.
Code:
Dim report As New ReportDocument
Dim connection As IConnectionInfo
Dim oldServerName As String = ".\SQLEXPRESS"
Dim oldDatabaseName As String = oldDatabaseName
Dim newServerName As String = newServerName
Dim newDatabaseName As String = newDatabaseName
Dim UserID As String = ""
Dim Password As String = ""
report.Load(Application.StartupPath + "\Reports\ValidationReport.rpt")
CrystalReportViewer1.ReportSource = report
'Change the server name and database in main report
For Each connection In report.DataSourceConnections
report.DataSourceConnections(oldServerName, oldDatabaseName).SetConnection(newServerName, newDatabaseName, UserID, Password)
Next
'Change the server name and database subreports
Dim subreport As ReportDocument
For Each subreport In report.Subreports
For Each connection In subreport.DataSourceConnections
If (String.Compare(connection.ServerName, oldServerName, True) = 0 _
And String.Compare(connection.DatabaseName, oldDatabaseName, True) = 0) Then
subreport.DataSourceConnections(oldServerName, oldDatabaseName).SetConnection(newServerName, newDatabaseName, UserID, Password)
End If
Next
Next
CrystalReportViewer1.RefreshReport()
Comment