473,587 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change Datasource in CrystalReport - VB.NET

115 New Member
I'm using VB.NET 2003 windows application.

i'm trying to display Crystal Reports using CrystalReportVi ewer.

Code i'm using to display "ZTab.rpt" in CrystalReportVi ewer 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 CrystalReportVi ewer 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.m db" 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.m db".

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 19977
OuTCasT
374 Contributor
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 New Member
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 Contributor
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
nivabeath
1 New Member
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
2913
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 chart that comes from a query. I have managed to do this in excel and embedding the worksheet. Program 2005 2006 2007 2008 2009 2010
0
1358
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... how can i void calling handler while setting datasource + displaymember + valuemember ???
3
15522
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 CrystalReportViewer and set it's ReportSource to my CrystalReport. When I run the program though nothing get's shown in my viewer. The following is how I...
2
8471
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 many features buried in the sample but all I want to know is a very simple thing, how to create bar charts with CrystalReport in VS.NET using SQL...
5
3066
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 open the reports and manually change the datasouce to the production database. Is there a way to set the datasouce on the webserviced reports...
6
9463
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 ?? Thanks in advance
0
997
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 changing this application to use XML files instead of using SQL Mobile database. What is the easiest way to change my dataset to instead use XML as...
3
2471
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 from Access database to SQL Server 2000 database.
0
1944
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 CrystalDecisions.CrystalReports.Engine.ReportDocument Dim connection As IConnectionInfo Dim OldServerName As String = Dim...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.