Connecting Tech Pros Worldwide Help | Site Map

Change Datasource in CrystalReport - VB.NET

Member
 
Join Date: Apr 2007
Posts: 99
#1: Jan 28 '09
I'm using VB.NET 2003 windows application.

i'm trying to display Crystal Reports using CrystalReportViewer.

Code i'm using to display "ZTab.rpt" in CrystalReportViewer when Form_Load
Expand|Select|Wrap|Line Numbers
  1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.           CrystalReportViewer1.ReportSource = "ZTab.rpt"
  3.           CrystalReportViewer1.Zoom(1)
  4. End Sub
  5.  
i have checkbox that contain list of crystal report. if i select one crystal report and hit button "Run", it displays that crystal report.
Code i'm using to display another (differenet) crystal report in CrystalReportViewer when Button (btnRun) clicked.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunNow.Click
  2.         CrystalReportViewer1.ReportSource = combobox1.SelectedItem
  3.         CrystalReportViewer1.Zoom(1)
  4. End Sub
  5.  
so when Form_load, it displays "ZTab.rpt" and when i select one crystal report in combobox and hit button "Run", it displays corresponding crystal report. that works fine.

how can i change the datasource of a report during run time using codes (programming).

for example: if we are using datasource "work.mdb" for "ZTab.rpt" when form_load. when i hit a button, how come i change the datasource to "Employee.mdb" for "ZTab.rpt" during run time using codes. so when form_load it will display ZTab.rpt with datasource "work.mdb" and when i hit a button, it will display ZTab.rpt with datasource "Employee.mdb".

If you have any idea how to do this, please help me. if you can provide an example then it will be great help for me.

Thanks in advance.
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#2: Jan 29 '09

re: Change Datasource in CrystalReport - VB.NET


You need to tell the crystal report that the datasource has changed.
You could change the database or even sql server if u want.

Expand|Select|Wrap|Line Numbers
  1.  
  2. If CheckBox1.Checked = True Then
  3. Dim connection As IConnectionInfo
  4. Dim oldServerName As String = ".\SQLEXPRESS"
  5. Dim oldDatabaseName As String = "Old Database Name"
  6. Dim newServerName As String = strOldServerName
  7. Dim newDatabaseName As String = strCompanyName
  8. Dim UserID As String = ""
  9. Dim Password As String = ""
  10.  
  11. Monthlyreport.Load(Application.StartupPath + "\Reports\EarningsSummaryRpt.rpt")
  12.  
  13. 'Change the server name and database in main report
  14. For Each connection In Monthlyreport.DataSourceConnections
  15. Monthlyreport.DataSourceConnections(oldServerName, oldDatabaseName).SetConnection(newServerName, newDatabaseName, UserID, Password)
  16. Next
  17. 'Change the server name and database subreports
  18. Dim subreport As ReportDocument
  19. For Each subreport In Monthlyreport.Subreports
  20. For Each connection In subreport.DataSourceConnections
  21. If (String.Compare(connection.ServerName, oldServerName, True) = 0 _
  22. And String.Compare(connection.DatabaseName, oldDatabaseName, True) = 0) Then
  23. subreport.DataSourceConnections(oldServerName, oldDatabaseName).SetConnection(newServerName, newDatabaseName, UserID, Password)
  24. End If
  25. Next
  26. Next
  27.  
This is how you would change the database and connection of the report
Member
 
Join Date: Apr 2007
Posts: 99
#3: Feb 4 '09

re: Change Datasource in CrystalReport - VB.NET


Thanks OutCast. That code worked. Thanks a lot.

Expand|Select|Wrap|Line Numbers
  1. Dim myreport As New ReportDocument
  2.     Dim strDSN As System.String
  3.     Dim strDB As System.String
  4.     Dim strUID As System.String
  5.     Dim strPWD As System.String
  6.    Private Sub frmMain_Load(ByVal sender........
  7.            CrystalReportViewer1.ReportSource = "ZTab.rpt"
  8.    End Sub
  9.    Private Sub btnChangeDataBase_Click(ByVal sender ........        
  10.         strNewPath = "C:\Projects\Employee.mdb"
  11.         myreport.Load("ZTab.rpt")
  12.         SetupReport(myreport)
  13.    End Sub
  14.    Private Function SetupReport(ByRef objCrystalReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument) As System.Boolean
  15.         Dim crTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo
  16.         Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database
  17.         Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables
  18.         Dim aTable As CrystalDecisions.CrystalReports.Engine.Table
  19.         Dim bTable As CrystalDecisions.CrystalReports.Engine.Table
  20.         Dim blnTest As System.Boolean
  21.         Dim strLocation As System.String
  22.  
  23.         crDatabase = objCrystalReportDocument.Database
  24.         crTables = crDatabase.Tables
  25.         For Each aTable In crTables
  26.             crTableLogOnInfo = aTable.LogOnInfo
  27.             strDSN = crTableLogOnInfo.ConnectionInfo.ServerName
  28.             strDB = crTableLogOnInfo.ConnectionInfo.DatabaseName
  29.             strUID = crTableLogOnInfo.ConnectionInfo.UserID
  30.             strPWD = crTableLogOnInfo.ConnectionInfo.Password
  31.             OutputDebugLine("BEFORE")
  32.             OutputDebugLine("TABLE NAME: " & aTable.Name)
  33.             OutputDebugLine("TABLE LOC: " & aTable.Location)
  34.             OutputDebugLine("SERVER: " & strDSN)
  35.             OutputDebugLine("DB: " & strDB)
  36.             OutputDebugLine("UID: " & strUID)
  37.             OutputDebugLine("PWD: " & strPWD)
  38.             OutputDebugLine("REPORT NAME: " & crTableLogOnInfo.ReportName)
  39.             OutputDebugLine("Table Name: " & crTableLogOnInfo.TableName)
  40.             aTable.ApplyLogOnInfo(crTableLogOnInfo)
  41.  
  42.             strLocation = strNewPath   'pass new mdb name
  43.  
  44.             OutputDebugLine("New Location: " & strLocation)
  45.             Try
  46.                 aTable.Location = strLocation
  47.             Catch ex As Exception
  48.                 OutputDebugLine("Set Location Error: " & ex.ToString)
  49.             End Try
  50.             OutputDebugLine("AFTER")
  51.             OutputDebugLine("TABLE NAME: " & aTable.Name)
  52.             OutputDebugLine("TABLE LOC: " & aTable.Location)
  53.             OutputDebugLine("SERVER: " & strDSN)
  54.             OutputDebugLine("DB: " & strDB)
  55.             OutputDebugLine("UID: " & strUID)
  56.             OutputDebugLine("PWD: " & strPWD)
  57.             OutputDebugLine("REPORT NAME: " & crTableLogOnInfo.ReportName)
  58.             OutputDebugLine("Table Name: " & crTableLogOnInfo.TableName)
  59.             Try
  60.                 blnTest = aTable.TestConnectivity()
  61.                 OutputDebugLine("CONNECTED? " & blnTest.ToString())
  62.             Catch ex As Exception
  63.                 OutputDebugLine("CONNECTED? NO")
  64.                 OutputDebugLine(ex.ToString)
  65.             End Try
  66.         Next aTable
  67.         myWrite.Close()
  68.         myWrite = Nothing
  69.         myFile = Nothing
  70.         CrystalReportViewer1.ReportSource = myreport
  71.     End Function
  72.  
  73.  
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#4: Feb 5 '09

re: Change Datasource in CrystalReport - VB.NET


Glad to be of some assistance. I struggled with this for quite a while so i know how frustrating it can be.
Reply