Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am building a Windows forms solution in VS2017 using VB.net. The back-end is a Access 2010 DB which resides on a file server for now, but will be deployed elsewhere eventually. I am trying to run a report passing multiple parameters dynamically. I am trying to do this one parameter at at time so it will make it easier to debug while I am building the report.

However I cant even get the report to run when I pass the first parameter. It runs off a query table in my DB and without parameters it shows the report, no problem. But as soon as I add any parameter I get an "Invalid Index" error. (System.Runtime.InteropServices.COMException: 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'

I am not a programmer (Use to be a VB programmer many years ago and I had to learn .Net in a matter of days to do this project and it has now been four months in the making.

Here is my Code Below:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Public Class frmViewReport

    Private Sub rptViewer_Load(sender As Object, e As EventArgs) Handles rptViewer.Load

        'On Error Resume Next

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

        cryRpt.Load(Application.StartupPath & "\CrystalReport2.rpt")

        With crConnectionInfo
            .ServerName = ELTDocumentFolder
            .DatabaseName = "\" & ELTDatabase
        End With

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

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue
        Dim crParameterDiscreteValue1 As New ParameterDiscreteValue

        'cryRpt.SetParameterValue("@Deal_Date", frmSelectReport.DateTimePicker1.Value)

        crParameterDiscreteValue.Value = frmSelectReport.DateTimePicker1.Value
        crParameterDiscreteValue1.Value = frmSelectReport.DateTimePicker2.Value
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields()
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("Deal_Date") '<<<ERROR OCCURS HERE
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterValues.Add(crParameterDiscreteValue1)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table
        Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo

        For Each tbCurrent In cryRpt.Database.Tables

            tliCurrent = Nothing

            With tliCurrent.ConnectionInfo

                .ServerName = Application.StartupPath
                .DatabaseName = Application.StartupPath & "\" & ELTDatabase

            End With


        Next tbCurrent

        rptViewer.ReportSource = cryRpt
        rptViewer.Refresh()

    End Sub

End Class


What I have tried:

I have been reading solutions all over the net for the last week. I have been copying and pasting code, checking the field names in the database, tried different fields
Posted
Updated 28-Mar-18 12:53pm
Comments
Richard Deeming 27-Mar-18 10:49am    
That error would suggest that CrystalReport2.rpt doesn't contain a parameter called Deal_Date.

Set a breakpoint on the line where the error occurs, and debug your code. Examine the crParameterFieldDefinitions collection, and look at the names of the parameters. The name needs to be an exact match to the string you pass to .Item(...).

At a guess, you probably need to include the "@" prefix:
crParameterFieldDefinition = crParameterFieldDefinitions.Item("@Deal_Date")

1 solution

As per the comment above by Richard, the Report cannot locate a parameter with the name "Deal_Date"
In my opinion you are better off looping through the Parameter fields and checking the name before you populate, the following will work;
VB
If(crRpt.DataDefinition.ParameterFields.Count > 0) Then
    For Each rptParam As ParameterFieldDefinition in crRpt.DataDefinition.ParameterFields
    ' check the reportname is empty - this ensures we set parameters in the main report only
        If(rptParam.Reportname = "") Then
            ' get the parameter name
            Dim strName As String = rptParam.ParameterFieldName
            ' check name and set value if correct
            If(strName = "Correct Name") Then
                Dim objVal as object = "My Value"
                crRpt.SetparameterValue(rptParam.ParameterFieldName, objVal)
            End If
        End If
    Next
End If


This will also help when debugging as you can actually see what the Report Parameter name is.

Kind Regards
 
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