473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Change Datasource in CrystalReport - VB.NET

115 100+
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.
Jan 28 '09 #1
4 19945
OuTCasT
374 256MB
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
Jan 29 '09 #2
remya1000
115 100+
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.  
Feb 4 '09 #3
OuTCasT
374 256MB
Glad to be of some assistance. I struggled with this for quite a while so i know how frustrating it can be.
Feb 5 '09 #4
Check th following link that create a subreport..

Crystal Report subreport

It will help you
Mar 25 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ghanley | last post by:
I have searched the web all day for a lead on this. I have found how to control the Graph object mut not the embedded excel unbound object frame. I am trying to chart the data below on one...
0
by: | last post by:
whenever you change datasource of an listbox it calls its handler... it make my program crash ... i did a bool if its in update it sets true so handler won't work but this is so stupid solution......
3
by: redneon | last post by:
I've populated a DataSet in my C# program and verified that it contains the data I passed it. I've then created a blank CrystalReport and set it's DataSource to my DataSet. Then I created a...
2
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so...
5
by: Ron | last post by:
I have a bunch of Crystal Reports (v9) published as WebServices and use a ReportViewer to display the reports on the ASPNET page. Everytime we move the reports from dev to production we have to...
6
by: Agnes | last post by:
Please help, i had completed over 30-50 reports, now. our client insist to change the database name. anyshorcut that I can change the database name in the report without re-write all of them ??...
0
by: Magnus Bergh | last post by:
I am working on an application for Pocket PC. When I started this I decided to use SQL Mobile and designed a few tables and created a typed dataset with the designer. Now I am thinking of...
3
by: mikewin86 | last post by:
Hello, May I ask a question. I have a vb.net window application project that connect to Access database. I use dataset for all Forms and Reports. Now I would like to change back end...
0
OuTCasT
by: OuTCasT | last post by:
Hi I dont understand why this code does not change the Crystal Reports Datasource and Database at runtime. I have used it before but for some reason it doesnt want to work. Dim report As New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.