I have created an application in VS 2005 pulling reports created in Crystal Reports XI Release 2. My problem is I have a couple of reports that have linked subreports on them. Does anyone know how to pass a parameter value to a subreport in .NET? I have the following code snippet and it works except that the same data appears in the subreport for all the main reports; unlinking the subreports.

I was originally told that you couldn't pass parameter values to a subreport - to create a parameter on the main report and link to the subreport - which is what I did, but now the reports don't seem to be linked.

Dim lpfdDefinitions As ParameterFieldDefinitions = objReport.DataDefinition.ParameterFields
Dim lpfdDefinition As ParameterFieldDefinition = lpfdDefinitions.Item(lSubreport.ParaName)

lpfdDefinition.ApplyCurrentValues(lSubreport.Values)

mobjReport.OpenSubreport(lSubreport.SubName).SetDataSource(lSubreport.DataTable)

mobjReport.SetParameterValue(lSubreport.ParaName, lSubreport.Values.ToArray())
mobjReport.SetParameterValue("PM-@" & lSubreport.ParaName, lSubreport.Values.ToArray(), lSubreport.SubName)

"lSubreport.Values" is loaded higher up in the code by values coming from the main report.
"lSubreport.DataTable" is the datatable that is loaded from the subreport's SQL Query. Any help with this would be greatly appreciated.

Cindy

For those of you who have the same issue, I have resolved my problem. The following is what I did to get the parameters to pass to the subreport correctly.

Within the subroutine that I get each parameter for each subreport:

For Each lSubreport As SubreportItems In mcolSubreports.Values
      Dim crSubReportDoc As ReportDocument

      crSubReportDoc = mobjReport.OpenSubreport(lSubreport.SubName)
      crSubReportDoc.SetDataSource(lSubreport.DataTable)

      Dim lpfdDefinitions As ParameterFieldDefinitions = crSubReportDoc.DataDefinition.ParameterFields
      Dim lpfdDefinition As ParameterFieldDefinition = lpfdDefinitions.Item("PM-" & lSubreport.ParaName)

      For Each lRow As DataRow In mobjData.Rows  '// I actually had to move this routine here for it to pass the all the values correctly.
            Dim lpdvValue As ParameterDiscreteValue = New ParameterDiscreteValue

            lpdvValue.Value = lRow("adID").ToString

            lpfdDefinition.CurrentValues.Add(lpdvValue)

            lSubreport.Values.Add(lpdvValue)
      Next

      lpfdDefinition.ApplyCurrentValues(lpfdDefinition.CurrentValues)

      mobjReport.SetParameterValue("PM-" & lSubreport.ParaName, lpfdDefinition.CurrentValues.ToArray(), lSubreport.SubName)

Next

This works great and I get the correct record linking the subreport with the main report.
--
Cindy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.