Hoping someone can help figure out what I am doing wrong.
I have a form that has combobox controls to allow the user to select PROGRAM and SHIP. These are stored in variables - me._rptProgram and me._rptShip. The user also selects a report name from a combobox control. This populates me._reportName. These are being successfully populated. No problem here
I have a "ViewReport" function that receives the location and name of my .rpt file along with the parameter names and values. When I step through this code - all variables, etc are being correctly populated
'to hold the number of parameters in the report definition for the report name passed 'in by the caller
Dim intCounter As Integer = 0
'to hold which table (stored procedure) currently
'looking at for subreport(s) (if report has any subreports)
Dim intCounter1 As Integer = 0
'create a new Crystal Reports report document object
Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'create a logon info object for the report
Dim conInfo As New CrystalDecisions.Shared.TableLogOnInfo
'parameter value object of report
'parameters used for adding the value to parameter
Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
'current parameter value object (collection) of cyrstal report parameters
Dim currValue As CrystalDecisions.Shared.ParameterValues
'sub report of this Crystal Report
Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
'sub report document of this Crystal report
Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'to hold the parameters passed in by the caller
Dim strParValPair() As String
'to hold the "current" parameter, used to process parameters one at a time
Dim strVal() As String
'to hold which parameter are we currently looking at and
'also used to hold which subreport section we are looking at
'(later in the code)
Dim index As Integer
Try
'load the report
objReport.Load(sReportName)
'check if there are parameters in the report
intCounter = objReport.DataDefinition.ParameterFields.Count
'Because parameter fields collection also picks the selection
'formula which is NOT a parameter, if total parameter
'count = 1, then we check whether it is a parameter or
'selection formula. If selection formula, set count to 0 (no parameters)
If intCounter = 1 Then
If InStr(objReport.DataDefinition.ParameterFields(0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
intCounter = 0
End If
End If
'if there are parameters in report (as designed) and user has passed them
'when calling this function, then split the parameter string and
'apply the values to the concurrent parameters
If intCounter > 0 And param.Trim <> "" Then
strParValPair = param.Split("&")
For index = 0 To UBound(strParValPair)
If InStr(strParValPair(index), "=") > 0 Then
strVal = strParValPair(index).Split("=")
paraValue.Value = strVal(1)
currValue = objReport.DataDefinition.ParameterFields(strVal(0).ToString.Trim).CurrentValues
currValue.Add(paraValue)
objReport.DataDefinition.ParameterFields(strVal(0)).ApplyCurrentValues(currValue)
End If
Next
End If
'set the connection information to ConInfo object
'so we can apply the connection information on each
'sp in the report
conInfo.ConnectionInfo.UserID = <substitute real user id here>
conInfo.ConnectionInfo.Password = <substitute real password here>
conInfo.ConnectionInfo.ServerName = <substitute real ServerName here>
conInfo.ConnectionInfo.DatabaseName = <substitute real DatabaseName here>
For intCounter = 0 To objReport.Database.Tables.Count - 1
objReport.Database.Tables(intCounter).ApplyLogOnInfo(conInfo)
Next
'loop through each section on the report then look through each object
'in the section, if the object is a subreport, then apply logon info
'on each sp of the sub-report
For index = 0 To objReport.ReportDefinition.Sections.Count - 1
For intCounter = 0 To objReport.ReportDefinition.Sections(index).ReportObjects.Count - 1
With objReport.ReportDefinition.Sections(index)
If .ReportObjects(intCounter).Kind = ReportObjectKind.SubreportObject Then
mySubReportObject = CType(.ReportObjects(intCounter), CrystalDecisions.CrystalReports.Engine.SubreportObject)
mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
mySubRepDoc.Database.Tables(intCounter1).ApplyLogOnInfo(conInfo)
Next
End If
End With
Next
Next
'if there is a selection formula passed to this function
'then use it
If sSelectionFormula.Length > 0 Then
objReport.RecordSelectionFormula = sSelectionFormula
End If
'Reset the control
CrystalReportViewer1.ReportSource = Nothing
'set the current report object to the viewer
CrystalReportViewer1.ReportSource = objReport
'Show the report
CrystalReportViewer1.Show()
Catch ex As Exception
<stuff will be here>
End Try
Return retval
End Function
Here is a piece of the code that calls this function. I only have one report so far, so the rest of the sub is skeleton and will fill in as I figure out where I've gone wrong.
Private Sub cmdRptExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRptExecute.Click
Try
Select Case Me._reportName
Case "Not Qcd"
Case "Requested vs Actual"
Me.ViewReport("C:\Projects\PriyaDNEHelper\Projects\PriyaDNEHelper\Reports\InspReqVsActual.rpt", , "@Program=" & Me._rptProgram.Trim & "&" & "@Ship=" & Me._rptShip.Trim)
Case "Summary By Est"
Case "Summary By Variety"
Case Else
MessageBox.Show("Please select a report")
End Select
Catch ex As Exception
<stuff will be here>
End Try
End Sub