Link to home
Start Free TrialLog in
Avatar of envirospatial
envirospatial

asked on

Crystal Reports passed parameters do not update report the first time sent vb.net

Hello,

I have a CR application that passes report parameters to a report.  The funny thing is the first time I try to send the parameters it does not update the report.  But the second time I send the parameters it works.   Hmm.. any ideas?
Thanks in advance
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If ListBox1.SelectedItems.Count = 0 Then
            MessageBox.Show("Selecionez al moins un parametre")
            Exit Sub
        End If
       
        Dim pReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        'Dim pReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim pParCollection As New CrystalDecisions.Shared.ParameterValues
        Dim pParVal As New CrystalDecisions.Shared.ParameterDiscreteValue
        Dim rpt_name As String
        rpt_name = "C:\SIDC - Asrir\Rapports\" + Trim(cb_rapport.Text) + ".rpt"
        
        Dim I As Integer
        For I = 0 To ListBox1.Items.Count - 1

            If ListBox1.GetSelected(I) = True Then
                pParVal = New CrystalDecisions.Shared.ParameterDiscreteValue   
                pParVal.Value = ListBox1.Items(I).ToString
                pParCollection.Add(pParVal)
            End If
        Next

        pReport.Load(rpt_name)

        If Trim(cb_rapport.Text).Contains("Partenaire") Then

            pReport.SetParameterValue("TypePart", pParCollection)
        Else
            pReport.SetParameterValue("IdProj", pParCollection)
            'pReport.DataDefinition.ParameterFields.Item("IdProj").ApplyCurrentValues(pParCollection)
        End If


        CrystalReportViewer1.ReportSource = pReport
        CrystalReportViewer1.Show()


    End Sub

Open in new window

Avatar of Mike McCracken
Mike McCracken

I susoect the report is saved with data.  That is the default setting in Crystal.

Try adding this line after you load the report
Make sure it is before the parameters are added to the report or they will be discarded also.

    pReport.DiscardSavedData

mlmcc
Avatar of envirospatial

ASKER

thanks for your comment mimcc.

My report is defined as follows:

Dim pReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument

I do not seem to have the DiscardSavedData method available.   To further detail: My app has a set of potential reports the users chooses a report from.  No matter which report it is, the first time the code is run,  the viewed report does not reflect the parameters: however, if the routine is run again ( or any time threre after) the viewed report has the correct parameters.  It seems as if something is not being initialized until after the first report is viewed.
I don't know.  There should be a method or property like that.

mlmcc
I checked again and I do not have the discard saved data method..

 I have saved the report without data, it still does not work the first time I run the report.  Once the  Viewer is opened( i.e. the code is run for the first time, with any of my reports) , it runs with the correct passed parameters.  

I still do not have an answer yet. Still lokking for help.

thanks
I don't know.  The code appears to be correct.

What are you getting?
A full report unfiltered?

mlmcc
Yes.  I am getting a full report.   At this point I am stumped...

any otehr suggestions are welcome..

ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Mimmcc,

thanks for the suggestion.

I have found a solution that does not feel right but it works.  If I show the viewer first , then load the report, then apply the report source to the viewer,then assign parameters and then apply the report source again to the viewer it works.  However I also ran into a problem with the CR parpameter prompter dialog popping up.  If I cancel the dialog, the report ran.  So I went back to the report itself and checked  the Save data with report option.  Once I did this the prompt dialog would not show up.

Final working code below..

Thanks for your help mimcc...
 If ListBox1.SelectedItems.Count = 0 Then
            MessageBox.Show("Selecionez al moins un parametre")
            Exit Sub
        End If
       
        Dim pReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim pParCollection As New CrystalDecisions.Shared.ParameterValues
        Dim pParVal As New CrystalDecisions.Shared.ParameterDiscreteValue
        Dim rpt_name As String
        rpt_name = "C:\SIDC - Asrir\Rapports\" + Trim(cb_rapport.Text) + ".rpt"
        Dim I As Integer
        For I = 0 To ListBox1.Items.Count - 1

            If ListBox1.GetSelected(I) = True Then
                pParVal = New CrystalDecisions.Shared.ParameterDiscreteValue   
                pParVal.Value = ListBox1.Items(I).ToString
                pParCollection.Add(pParVal)
            End If
        Next
        CrystalReportViewer1.Show()
        pReport.Load(rpt_name)
        CrystalReportViewer1.ReportSource = pReport
        If Trim(cb_rapport.Text).Contains("Partenaire") Then
            pReport.SetParameterValue("TypePart", pParCollection)
        Else
            pReport.SetParameterValue("IdProj", pParCollection)
            'pReport.DataDefinition.ParameterFields.Item("IdProj").ApplyCurrentValues(pParCollection)

        End If

        CrystalReportViewer1.ReportSource = pReport

Open in new window

Not sure I like the solution but if it works.

mlmcc