Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I created crystal reports using stored procedures and specified source connection in wizard while creating reports. Now how can i change data connection of these reports at runtime, when I run my application on client PC as they are directing to connection source to Database in my system. following is connection string of my database and code am using to display reports. kindly help. thanks in advance.


Data Source=WAQAS-PC\sqlexpress;Initial Catalog=DailyAccounting;Integrated Security=True;
VB
Private Sub btnShowByDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowByDate.Click
    Try
        Dim objReport As New rptPatientsByDate()
        objReport.SetParameterValue("@fromDate", dtFromDate.Text.Trim)
        objReport.SetParameterValue("@toDate", dtToDate.Text.Trim)
        Dim obj As New frmReportViewer
        obj.rptViewer.ReportSource = objReport
        obj.ShowDialog()
    Catch ex As Exception
        MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
Posted

I struggled with this awhile. I believe it depends on your version of Crystal and possibly on the version of Crystal in which your reports were created. The following, simple and succinct, used to work:

VB
For iLoop = 0 To obj.rptViewer.LogOnInfo.Count - 1
                obj.rptViewer.LogOnInfo(iLoop).ConnectionInfo.ServerName = server
                obj.rptViewer.LogOnInfo(iLoop).ConnectionInfo.UserID = user
                obj.rptViewer.LogOnInfo(iLoop).ConnectionInfo.Password = password
            Next iLoop


If that doesn't work, let me know, and I'll give you another option. Or maybe someone else can tell us if they've done this consistently across versions. In my experience, Crystal broke compatibility around 2008, and then broke it again (or maybe they'd say "fixed" it) in 2010 and/or 2013.
 
Share this answer
 
Comments
Waqas Ahmad Abbasi 15-Nov-13 0:47am    
doesn't work, still directing to my system DB.
OK, so let's try what, as I understand it, is the new way to go. It's best documented on Crystal's site here[^] and here[^], and you can also find good information in their help file "To change a persistent data source" (xi4_rassdk_net_dg_en.chm::/html/topic82.html, available here[^], if you don't have it).

The upshot is that you have to go at it a totally different route through a new library, replacing every existing connection in the report with new information. You should already have the new library, assuming you're using a full install of the Crystal components--if not, I'm pretty sure they're freely available in one of Crystal's SDKs. Add project references to CrystalDecisions.ReportAppServer.Controllers and CrystalDecisions.ReportAppServer.DataDefModel and then try something like the following:

VB
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportAppServer

...

Dim rpt As New ReportDocument
RASReplaceCRConnection(rpt.ReportClientDocument, rpt.DataSourceConnections(0).Attributes)

...

    Private Sub RASReplaceCRConnection(ByRef rcd As ClientDoc.ISCDReportClientDocument, atts As CrystalDecisions.Shared.DbConnectionAttributes)
        Dim rpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        'reset connection
        rcd.DatabaseController.LogonEx(yourServer, "", yourUser, yourPassword)

        'Create the QE (query engine) propertybag with the provider details and logon property bag
        Dim QE_Details As New DataDefModel.PropertyBag
        For Each nvp As CrystalDecisions.Shared.NameValuePair2 In atts.Collection 'copy from existing attributes except for server name
            Select Case nvp.Name.ToString
                Case "QE_ServerDescription"
                    QE_Details.Add(nvp.Name, yourServer)
                Case "QE_LogonProperties"  'this is itself a property bag
                    Dim logonDetails As New DataDefModel.PropertyBag
                    For Each nvp2 As CrystalDecisions.Shared.NameValuePair2 In DirectCast(nvp.Value, CrystalDecisions.Shared.DbConnectionAttributes).Collection
                        Select Case nvp2.Name.ToString
                            Case "Data Source"
                                logonDetails.Add(nvp2.Name, yourServer)
                            Case Else
                                logonDetails.Add(nvp2.Name, nvp2.Value)
                        End Select
                    Next
                    QE_Details.Add(nvp.Name, logonDetails)
                Case Else
                    QE_Details.Add(nvp.Name, nvp.Value)
            End Select
        Next

        'now replace all existing connections with new one
        Dim newConnInfo As New DataDefModel.ConnectionInfo With {.Attributes = QE_Details,
                                                                                                  .Kind = DataDefModel.CrConnectionInfoKindEnum.crConnectionInfoKindCRQE,
                                                                                                  .UserName = yourUser,
                                                                                                  .Password = yourPassword}
        For Each oldConnInfo As DataDefModel.ConnectionInfo In rcd.DatabaseController.GetConnectionInfos
            rcd.DatabaseController.ReplaceConnection(oldConnInfo, newConnInfo.Clone, Nothing, DataDefModel.CrDBOptionsEnum.crDBOptionDoNotVerifyDB)
        Next
    End Sub


Painful, but worked for me. Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900