i am using b.net 2005. i am generating a crystal report. but when i work in a different computer i have to set the location of the database of the report before i generate. is their any dynamic way to set my database location to the report.
is their any dynamic way to set my database location to the report.
Collapse
X
-
Changing Database of Crystal Report
I use a sql query to get the ServerName of the current machine that i am on.
Once i have done that then i can change the server for the report before its loaded on the crystal report viewer.Code:Private Sub GetName() Dim sqlcon As SqlConnection = New SqlConnection("Server=(local);Data Source=.\sqlexpress;Initial Catalog='" & strCompanyName & "';Integrated Security=True") Dim sqlCOm As SqlCommand = New SqlCommand("select @@servername", sqlcon) sqlcon.Open() strOldServerName = sqlCOm.ExecuteScalar sqlcon.Close() End Sub
What i did in the oldDatabaseName is use the databaseName that i created the report on, seems to get a problem if u dont do this.
All you do is set the location of ur report. You will see i have my in my application.sta rtuppath.
Code:Private Sub Report() Dim report As New ReportDocument Dim connection As IConnectionInfo Dim oldServerName As String = "DONOVANPC\SQLEXPRESS" Dim oldDatabaseName As String = "PayDay10" Dim newServerName As String = strOldServerName Dim newDatabaseName As String = strCompanyName Dim UserID As String = "" Dim Password As String = "" report.Load(Application.StartupPath + "\Reports\PerksTaxReport.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() End Sub
Comment
Comment