cancel
Showing results for 
Search instead for 
Did you mean: 

How to persist programatically set parameter values in WpfViewer parameter prompt?

Former Member
0 Kudos

We are using Visual Studio 2013 Update 4; Crystal 13.0.12.1494.  Here is the problem, and we have created a simple project that demonstrates our issue.  WPF project with 1 form with the viewer.  Load a report with an Oracle datasoure that just reads the data dictionary for a database table names.  1 report parameter to filter by table name.  In the report file, the parameter is set to find 'TABLE1'.  Right after loading the report, and before we assign it to the viewer, we need to change the parameter to 'TABLE2'.  This works okay.  The problem is then when the viewer refresh is clicked, and we prompt for new parameters, the parameter value reverts to back to the orignial from the report file 'TABLE1'.  I need it to persist the values I had programatically set, 'TABLE2'. We have reports with dozens of parameters, our .Net application has the correct initial parameter values, not the report file, and it ins't viable for us to set them to anything of significance in the report file, they aren't always known until the user is in a particular part of our application.  This is a major issue for us, any help is appreciated.  Below is the codebehind from our simple c# example project.

Thanks,

Dominic.

    public partial class Window1 : Window
    {
        CrystalDecisions.CrystalReports.Engine.ReportDocument Report;

        public Window1()
        {
            InitializeComponent();
            reportViewer.Owner = this;

            Report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            Report.Load("Crystal_Test.rpt");
            Report.SetParameterValue("TABLE_NAME", "TABLE2");
            reportViewer.ViewerCore.ReuseParameterWhenRefresh = false;
            reportViewer.ViewerCore.ReportSource = Report;
        }

    }

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Dominic,

Normally Parameters cannot be used to determine which Table the report goes to. For that you need to set the table location using:

report.SetTableLocation and fully qualify it.

So need more info, is this a Store Procedure that is setting the table name?

If you set ..Refresh = True, does that work? thinking that since you have set the values you don't want to set the to false and use the previous values.

Need more details about your report, can you attach it?

Don

Former Member
0 Kudos

Thank you for the reply, let me clarify.  My simple project was querying the data dictionary, words like 'TABLE' for parameter values are confusing.  The report connects to an Oracle database, that is all configured in the report file.  In more generic terms here is what happens.  The report has 1 parameter, let's call 'Parameter1' instead.  In the report file, the value for Parameter1 is 'Value1'.  So in a simple code project, I load the report file, then change the parameter value, e.g. SetParameterValue('Parameter1', 'Value2').  Then I assign the report to the viewer.  I then see that the report that is now in the viewer ran with 'Value2' for Parameter1; looks great.  I then click Refresh in the viewer, and then click 'No' to prompt for the parameters so I can change them.  This is where the problem is, the viewer then pops the parameter prompt, and the value it shows for Parameter1 is 'Value1'  !?!?!  I changed it to 'Value2' programatically, it had run with Value2; why does the prompting value revert to the rpt file?  At that first refresh prompt, I need the parameter values to keep the values I had set programatically before I handed it off to the viewer.

Let me know if the issue still is not clear, and I appreciate any help.

Dominic.

0 Kudos

Hi Dominic,

Ah, that makes more sense now, thanks for clarifying...

Try this code, note you have to detect if the Param is a CR or SP type:

private void button4_Click(object sender, RoutedEventArgs e)
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    doc.Load(@"C:\reports\ParamNET.rpt");

    try
    {
        foreach (CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition pField in (ParameterFieldDefinitions)doc.DataDefinition.ParameterFields)
        {
            if (pField.ParameterType == ParameterType.StoreProcedureParameter)
            {
                doc.SetParameterValue("@percentage", "100");
            }
            if (pField.ParameterType == ParameterType.ReportParameter)
            {
                SetCrystalParam(doc, "@CRParam", "1004");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

    try
    {
        crystalReportsViewer2.ViewerCore.ReportSource = doc;
        crystalReportsViewer2.ViewerCore.Visibility = Visibility.Visible;
        //crystalReportsViewer2.ShowPrintButton = true;
    }
    catch (IOException ex)
    {
        MessageBox.Show("Error: " + ex.Message);
        return;
    }
}
private void SetCrystalParam(CrystalDecisions.CrystalReports.Engine.ReportDocument rpt, string parameterName, string listOfValues)
{
    int ParCount = rpt.DataDefinition.ParameterFields.Count;

    foreach (CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition crParamField in rpt.DataDefinition.ParameterFields)
    {

        if ("@" + crParamField.Name.ToLower() == parameterName.ToLower())
        {
            try
            {
                CrystalDecisions.Shared.ParameterValues myparameterValues = new CrystalDecisions.Shared.ParameterValues();
                CrystalDecisions.Shared.ParameterDiscreteValue crDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();

                crDiscreteValue.Value = listOfValues;
                myparameterValues.IndexOf(crParamField.Name);
                myparameterValues.Add(crDiscreteValue);
                crParamField.ApplyCurrentValues(myparameterValues);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
    }
}

There are other types also but these 2 should be all you need.

Don

Former Member
0 Kudos

Don,

Apprecieate the reply, but it doesn't work, it is the same behavior.  Report runs fine with the set values, when I refresh and prompt for new parameters, the operative value in the prompt is still from the one from the rpt file. Any other ideas?  Again, much appreciate you looking in to it.

Dominic.

0 Kudos

Hi Dominic,

It worked for me but I don't recall now if I had SP 12 or the last SP 13 build installed. Next time I go back to SP 12 I'll test it and confirm it works for me.

Can you attach your report, maybe something different in the way we created the reports?

Don

Former Member
0 Kudos

Well...it seems rpt files are prohibited attachment types in the forum, maybe that deserves it's own new discussion thread.

There really is nothing to the report, a single string parameter with a default value, an oracle database command 'SELECT * FROM SOME_TABLE WHERE' and use the parameter for filter.

If you have another way to get the file to you, I'll oblige.

Thanks for keeping on it.

0 Kudos

Hi Dominic,

Yes that is odd that you cannot attach a RPT file, I've asked for an update and no reason given why not...

Anyways, just rename it from *.rpt to *.txt and then you can attach it. Do not zip it up either, the system will zip it for you.

Don

Former Member
0 Kudos

Don,

Thanks as always for the reply.  Attached is the renamed rpt file.

Dominic.

Answers (0)