Print Page | Close Window

Issue when trying to change database dynamicly

Printed From: Crystal Reports Book
Category: Crystal Reports for Visual Studio 2005 and Newer
Forum Name: Writing Code
Forum Discription: .NET programming API, report integration
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=14686
Printed Date: 04 May 2025 at 12:23am


Topic: Issue when trying to change database dynamicly
Posted By: johmolan
Subject: Issue when trying to change database dynamicly
Date Posted: 16 Oct 2011 at 11:42pm
I have a report made in vs2008, it is a main report with 9 sub-reports. all the sub-reports have parameters. 1 uses KundeID and the rest uses OrdreID. I hva linked the parametres in the subreports to parameters in the main report this way it only prompts for the 2 parameters when I run the report in crystalreportviewer using this code:

Dim crp As New CrystalReport3 Me.CrystalReportViewer1.ReportSource = crp

But when I try to change the logoninfo dynamicly and set the parameters from the vb-code it only prompts for the KundeID and it ordredoes so several times, byt is does not prompt for the OrdreID parameter and no data is shown in the subreports using this parameter.

My code lookd like this:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Configuration

Public Class CR Private Sub CR_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cryRpt As New ReportDocument
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables

Dim CrTable As Table

cryRpt.Load("Reports/CrystalReport5.rpt") '***************************************************************
'Dim Sname As String
'Dim Dbname As String
'Dim Uname As String
'Dim Pwd As String 'Dim kid As Integer = 2
'Dim oid As Integer = 1
'Sname = CRdb.Servadr
'Dbname = CRdb.Servname
'Uname = CRdb.Username
'Pwd = CRdb.Passw
'MsgBox("Sname = " & Sname)
'MsgBox("Dbname = " & Dbname)
'MsgBox("Uname = " & Uname)
'MsgBox("Pwd = " & Pwd) '****************************************************************

With crConnectionInfo
.ServerName = "JOHNMOLANDS-PC\SQLEXPRESS"
.DatabaseName = "Kalkyle1"
.UserID = "User2"
.Password = "user2"
End With

Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As ParameterValues
Dim crParameterDiscreteValue As ParameterDiscreteValue crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields

'Begin(parameter)
crParameterFieldDefinition = crParameterFieldDefinitions.Item("KundeID")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterDiscreteValue = New ParameterDiscreteValue
crParameterDiscreteValue.Value = 1
crParameterValues.Add(crParameterDiscreteValue) crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'End parameter

' Begin parameter
crParameterFieldDefinition = crParameterFieldDefinitions.Item("OrdreID")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterDiscreteValue = New ParameterDiscreteValue
crParameterDiscreteValue.Value = 1 crParameterValues.Add(crParameterDiscreteValue) crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
' End parameter CrTables = cryRpt.Database.Tables

For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next

CrystalReportViewer1.ReportSource = cryRpt

'*************************************************************************** '*************************************************************************** '******************************************************************************
End Sub
End Class



I also tried to put the parameter chang code into the For loop but then I got an error that this changes can not be applied to a linked parameter.

Can anyone help me??



Replies:
Posted By: CsharpCoding
Date Posted: 17 Feb 2012 at 10:22am
you have to link the subreports tables to the datasource to
 
// please convert C# to VB yourself

// link the tables for the subreports

           

CrystalDecisions.Shared.TableLogOnInfo currentTableLogOn;

CrystalDecisions.Shared.ConnectionInfo cnCurrentConnectInfo;

// set here the connection parameters
 
foreach(CystalDecisions.CrystalReports.Engine.ReportDocument subReport in crReport.Subreports)

{

    foreach (CrystalDecisions.CrystalReports.Engine.Table reportTable in subReport.Database.Tables)

    {

       currentTableLogOn = reportTable.LogOnInfo;

       currentTableLogOn.ConnectionInfo = cnCurrentConnectInfo;

       reportTable.ApplyLogOnInfo(currentTableLogOn);

      }

}




Print Page | Close Window