Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > Crystal Reports
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Crystal Reports General discussion about Crystal Reports. For discussions specific to the book Professional Crystal Reports for VS.NET, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Crystal Reports section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 27th, 2009, 11:15 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Change Datasource in CrystalReport - VB.NET

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

Code:
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            CrystalReportViewer1.ReportSource = "ZTab.rpt"
            CrystalReportViewer1.Zoom(1)

    End Sub


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.

Code:
    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunNow.Click

        CrystalReportViewer1.ReportSource = combobox1.SelectedItem
        CrystalReportViewer1.Zoom(1)

    End Sub


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.
Reply With Quote
  #2 (permalink)  
Old February 4th, 2009, 01:05 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This code works for me. Hope this helps someone...
Code:
    Dim myreport As New ReportDocument
    Dim strDSN As System.String
    Dim strDB As System.String
    Dim strUID As System.String
    Dim strPWD As System.String
   Private Sub frmMain_Load(ByVal sender........
           CrystalReportViewer1.ReportSource = "ZTab.rpt"
   End Sub
   Private Sub btnChangeDataBase_Click(ByVal sender ........        
        strNewPath = "C:\Projects\Employee.mdb"
        myreport.Load("ZTab.rpt")
        SetupReport(myreport)
   End Sub
   Private Function SetupReport(ByRef objCrystalReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument) As System.Boolean
        Dim crTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo
        Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database
        Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables
        Dim aTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim bTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim blnTest As System.Boolean
        Dim strLocation As System.String
        
        crDatabase = objCrystalReportDocument.Database
        crTables = crDatabase.Tables
        For Each aTable In crTables
            crTableLogOnInfo = aTable.LogOnInfo
            strDSN = crTableLogOnInfo.ConnectionInfo.ServerName
            strDB = crTableLogOnInfo.ConnectionInfo.DatabaseName
            strUID = crTableLogOnInfo.ConnectionInfo.UserID
            strPWD = crTableLogOnInfo.ConnectionInfo.Password
            OutputDebugLine("BEFORE")
            OutputDebugLine("TABLE NAME: " & aTable.Name)
            OutputDebugLine("TABLE LOC: " & aTable.Location)
            OutputDebugLine("SERVER: " & strDSN)
            OutputDebugLine("DB: " & strDB)
            OutputDebugLine("UID: " & strUID)
            OutputDebugLine("PWD: " & strPWD)
            OutputDebugLine("REPORT NAME: " & crTableLogOnInfo.ReportName)
            OutputDebugLine("Table Name: " & crTableLogOnInfo.TableName)
            aTable.ApplyLogOnInfo(crTableLogOnInfo)
            
            strLocation = strNewPath   'pass new mdb name
           
            OutputDebugLine("New Location: " & strLocation)
            Try
                aTable.Location = strLocation
            Catch ex As Exception
                OutputDebugLine("Set Location Error: " & ex.ToString)
            End Try
            OutputDebugLine("AFTER")
            OutputDebugLine("TABLE NAME: " & aTable.Name)
            OutputDebugLine("TABLE LOC: " & aTable.Location)
            OutputDebugLine("SERVER: " & strDSN)
            OutputDebugLine("DB: " & strDB)
            OutputDebugLine("UID: " & strUID)
            OutputDebugLine("PWD: " & strPWD)
            OutputDebugLine("REPORT NAME: " & crTableLogOnInfo.ReportName)
            OutputDebugLine("Table Name: " & crTableLogOnInfo.TableName)
            Try
                blnTest = aTable.TestConnectivity()
                OutputDebugLine("CONNECTED? " & blnTest.ToString())
            Catch ex As Exception
                OutputDebugLine("CONNECTED? NO")
                OutputDebugLine(ex.ToString)
            End Try
        Next aTable
        myWrite.Close()
        myWrite = Nothing
        myFile = Nothing
        CrystalReportViewer1.ReportSource = myreport
    End Function
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change datasource of crystal report at runtime check123 Pro VB 6 1 March 21st, 2008 05:45 AM
change Datasource of a crystal report at run time tiyyob Pro VB 6 1 February 17th, 2007 06:05 AM
Datasource Change tjmherman Crystal Reports 2 August 2nd, 2006 10:24 AM
CrystalReport.net VisualStudio 2005 ASP.net (VB) pgkdave Crystal Reports 0 June 16th, 2006 09:39 AM
Change line in VB.net Dinesh22 VB.NET 2002/2003 Basics 2 January 16th, 2004 02:27 PM



All times are GMT -4. The time now is 12:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.