I am doing a crystal report, and I would like to dinamiclly add parameters.

I did the code which adds the value to one parameter, which I have created in the crystalReportViewer:

CrystalReport1 cr = new CrystalReport1();
cr.SetParameterValue("ParameterName", ParameterValue);
crystalReportViewer1.ReportSource = cr;
crystalReportViewer1.Refresh();

But this is for only one value.

Now I would like to put more values for that ParameterName. This is the code I fould on this forum, but the problem is that I do not know what to pass for "ParameterFields paramFields":

This is where I get the ParameterNames:

foreach (string myParamName in allParamNames)
{
     AddParameter("myParameterField", myParameterName, /*WHAT COMES HERE? - to pass the 3rd value to the method bellow*/ );
}

private ParameterFields AddParameter 
   (string paramName, string paramValue,
   ParameterFields paramFields) //I do not know what to pass to here!
{
   ParameterField paramField= new ParameterField ();
   ParameterDiscreteValue paramDiscreteValue = new 
   ParameterDiscreteValue ();
   ParameterValues paramValues = new ParameterValues ();

   // Set the name of the parameter <strong class="highlight">to</strong> modify.
   paramField.ParameterFieldName = paramName;

   // Set a value <strong class="highlight">to</strong> the parameter.
   paramDiscreteValue.Value = paramValue;
   paramValues.Add (paramDiscreteValue);
   paramField.CurrentValues = paramValues;

   // Add the parameter <strong class="highlight">to</strong> the ParameterFields collection.
   paramFields.Add (paramField);
   return paramFields;
}

Recommended Answers

All 34 Replies

Or I did something like this, but it is still not showing all the values, only the last one:

ParameterValues myParams = new ParameterValues();
            ParameterDiscreteValue pdv = new ParameterDiscreteValue();
            foreach (string myValue in AllMyValues)
            {
                pdv = new ParameterDiscreteValue();
                pdv.Value = myValue.ToString();
                myParams.Add(pdv);
            }
            cr.SetParameterValue("ParameterName", myParams);
            crystalReportViewer1.Refresh();

Whats am I doing wrong, that the values are not shown? Why there is only the last one shown? I have set the parameter (parameter name is "ParameterName") option "Allow multiple values" to true (checked it).

EDIT:
As fas as I read I need to write a formula which can have multiple lines and it can grow. What to write it? I presume it goes to Forumla Field.

No one knows how to salve this problem of adding more values to one parameter? Do I need to add any formula?
If so, how its gonna look? And you have to know I dont work with database in this example.

I still didnt salve the problem. I am struck with the code 3 post above.
I can no succeed how to pass more values (to be shown one under another) in just one parameter.


Can someone help me out please?

>how to pass more values (to be shown one under another) in just one parameter.

cryRep.SetParameterValue("@data", new int[]{10,20,30,40});

>how to pass more values (to be shown one under another) in just one parameter.

cryRep.SetParameterValue("@data", new int[]{10,20,30,40});

I tried that before and today ones again, but it is showing ONLY the 1st value (in this case only number 10). There is no others. I am missing sometihng, something that will tell to the crystal report viewer that there is are multiple values. And I have checked the option "Allow multiple values" to true, so there has to be sometihng else. But what?

>but it is showing ONLY the 1st value..

Create a formula field to check the value,
{?parameter}[index]

{?data} [1]  <- 1st value
 {?data} [2]  <- 2nd value
  ....

>but it is showing ONLY the 1st value..

Create a formula field to check the value,
{?parameter}[index]

{?data} [1]  <- 1st value
 {?data} [2]  <- 2nd value
  ....

Ok, but would you be please so kind and just explain me a bit more, that I would know what do I need to do this? What do I need a formula field?

For code belove that I just simply do as you did:

{?data} [1]
{?data} [2] .. or
{?data} [3]

to define where new value has to be placed?


And one more thing, how to connect this formula filed with The parameter (?parameter)?


This is how I did, but its not correct (i guess there is something missing):
http://file.si/files/2150_le4nn/C%23_crystalReport03_parametri.JPG

There is something bothering me.
I have a "Parameter" which I bind to values that _I have to show on the report. So if I have to show more values one under another I have to do a formula field parameter. But I can not put this formula field parameter onto the report. There already is "Parameter".
I am not clear about how to connect these two parameters?

Hi Mitja,

You need to concatenate parameter array to build a formula field,

This is a code of formula field. Have a look,

{?data} [1] + ' ' + {?data}[2] + ' ' + {?data}[3]

And this is the code that set parameter value,

...
crRep.SetParameterValue("data", new string[]{"One","Two","Three"});
...

Thx, but again it shows only the 1st value! There are not 3 of them in the list (one under another).

I am still missing something, but what. Do I need to create aditional group field, to show the list of values? Becuase I have them now in Section3 (details). Could this be the reason of it?

Maybe the section is not extanding ? How to set one section (in my case I have Section3 -Details) to extend if more values need to be shown? Does crystal report do this automatically?

Is there anyone who would know why I still get only one value into parameter, even if I wrote the formula field, like it was explained in the upper posts.

Please.

Can I have .zip file (do not add executable/binary) of your work?

Just sent you a private message.
Big thanks.

Mitja,

Asking help via PM or email is against the Daniweb member rules.

Read rules : http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Keep It On The Site
Do not post asking for an answer to be sent to you via email or PM. Problems and their responses assist others who read them. Please do not email or PM forum staff with your support questions.

I know its against the rules, I Just didn't want to post my project here. You asked me if I send you, so I did. Do I have to post it here again, or not? Would you look into my project what is wrong?

>You asked me if I send you, so I did.

I think you have no idea about "Attachment" option here (Click on Replay To Thread. Below the code editor you will find "Manage Attachments" button).

I have included only those classes in which I have the code for showing the values in the crystal report viewer.

I am attaching code with this post. I hope it will help you.

One question for the report3:

Lets take a look at this code:

private void ViewThree_Load(object sender, EventArgs e)
        {
            DS a = new DS();
            string[] items = { "AA", "BB", "CC" };
            
            ReportThree1.SetDataSource(a);
            ReportThree1.SetParameterValue("data", items);

        }

Why you do not use DataSet a for values in the "data" parameter like:

ReportThree1.SetParameterValue("data", a);

This is how I have?
And maybe this is what I am doing wrong.

>Why you do not use DataSet a for values in the "data" parameter

Crystal report parameter value could be single value or an array.

I did work a bit more with putting values into formula field on the report. And I got the values into them.

But I have one problem:

I did in the formula field:

{?Data} [1] + ' ' + {?Data} [2] + ' ' + {?Data} [3] -> This means the values are written into one line


if I did
{?Data} [1] + ' ' +
{?Data} [2] + ' ' +
{?Data} [3] -> I again get values written into one line


I would like to get the values written one under another.


How would I write the code for the into value empty:

If IsNull({?Storitve} [3]) OR {?Storitve} [3] = '' Then "Empty Parameter"
Else If{?Storitve} [3]

>I would like to get the values written one under another. How?

Create three formula fields where each formula field represent a single array element.

formula_field1

{?data}[1]

formula_field2

{?data}[2]

formula_field3

{?data}[3]

Drop these three formula fields onto the any section of crystal designer one under another.

I did that, and if I fill all three fields with values its all working fine.
But the error occurs when there is no value in the parameter. See attached image

On the 2nd image you can see how is my report made. In te 3rd section (details) I have put the formula fields.

Check the bound of an array in formula field

if UBound ({?data})=3 then {?data}[3]

What exactly means "UBound" ?
As far as I read its an array of values, right?

I have put this code in all the formula fields.
Becuase now if I want to show two values out of all three, there is no more error, but I got written only the 2nd value (there is no the 1st).

And if I want to show all three values, there is only the 3rd one shown.
There is no 1st and 2nd values.

Sorry for asking so much, but this is my 1st time doing this, and I would like to learn it.

>What exactly means "UBound" ?

Upper limit (boundary) of an array. Crystal report array has value 1 as lower bound.

>I got written only the 2nd value (there is no the 1st).

This should not be a problem. Do not use multi-valued parameter. Use single value parameter.


>I got written only the 2nd value (there is no the 1st).

This should not be a problem. Do not use multi-valued parameter. Use single value parameter.

This is not working. I have an array of values for the parameter. So it has to allow multiple values.
The only way would be to have for every value its own parameter. But this can not be - its too long code. How in case if I would like to pass 100 values? There is got to be some other way to pass an array of values to just ONE parameter. I think so.

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.