Search code examples
c#crystal-reports

How to pass multiple data to crystal report parameter


How can I pass multiple values to a single parameter in crystal report?

enter image description here

In my Qty field I want it to be my input values. How can I achieve that?

int[] value = new int[listProducts.Items.Count];
        int[] mylist = new int[listProducts.Items.Count];
        foreach (ListViewItem item in listProducts.Items)
        {
            mylist = value.Select(I => int.Parse(item.SubItems[2].Text)).ToArray();
        }
returnGood.SetParameterValue("_Qty", mylist);

This only display 1 value in my report I input 13 and 15 but only 13 is showing.


Solution

  • You can input a string of values into a Parameter Field using a delimiter. Then use the Split() function in your report to parse the Parameter Field into an array.

    For example, if you have a Parameter Field named MyParam and passed the values into the parameter as 1,2,3,4 then the result of this would be a parameter with a string value of "1,2,3,4". Then to create an array of string values you could use the following in a Formula Field.

    Stringvar Array myArray := Split({?MyParam}, ",");
    MyArray[1];
    

    This formula would then print the first value of the array. Changing the index for MyArray would allow you to print other values of the array.

    I do want to advise caution with this approach though. Arrays are a challenge to work with like this because of a few limitations of Crystal Report. For example, a formula field may not return an array and will always require you to specify a specific indexed value to return as a scalar result of the formula. So when you don't know how many values to expect in the array it can become rather complex in logic to know how to deal with all of the values passed into the Parameter. The Parameter Field's value must always be a string for this to work as well. So if you need the individual values of the array parsed as a different data type you will need to convert them to those data types and store them in new variables after the Split() function has been used.

    If you know how many values to expect in the single Parameter Field from the start, its usually a better idea to setup multiple Parameter Fields from the onset since they are much easier to work with in a crystal report.