Hello everybody,
Is that posible to Add parameters to crystal report programmatically.
I mean adding not passing.
Thanks.

Hi,
Thanks for your reply.
But when i tried to implement that it throws exception:-
Specified argument was out of the range of valid values.

That's the code :-

private void SetCurrentValuesForParameterField(ParameterFields parameterFields)
        {
            ParameterValues currentParameterValues = new ParameterValues();
            foreach (string str in td.Values)
            {
                ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
                parameterDiscreteValue.Value = str;
                currentParameterValues.Add(parameterDiscreteValue);
            }
         ParameterField parameterField = parameterFields[PARAMETER_FIELD_NAME];------  here i get the exeption
            parameterField.CurrentValues = currentParameterValues;
        }

the code to bind reportviewer control:-

ParameterFields parameterFields = crystalReportViewer1.ParameterFieldInfo;
            SetCurrentValuesForParameterField(parameterFields);
private void SetCurrentValuesForParameterField(ParameterFields parameterFields)
        {
            ParameterValues currentParameterValues = new ParameterValues();
            foreach (string str in td.Values)
            {
                ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
                parameterDiscreteValue.Value = str;
                currentParameterValues.Add(parameterDiscreteValue);
            }
         ParameterField parameterField = parameterFields[PARAMETER_FIELD_NAME];------  here i get the exeption
            parameterField.CurrentValues = currentParameterValues;
        }

the code to bind reportviewer control:-

ParameterFields parameterFields = crystalReportViewer1.ParameterFieldInfo;
            SetCurrentValuesForParameterField(parameterFields);

Hi Wafaa. I looks like you added the param value, but did not add the new parameter to the field collection. See this MSDN example:

private ParameterFields AddParameter 
   (string paramName, string paramValue,
   ParameterFields paramFields)
{
   ParameterField paramField= new ParameterField ();
   ParameterDiscreteValue paramDiscreteValue = new 
   ParameterDiscreteValue ();
   ParameterValues paramValues = new ParameterValues ();

   // Set the name of the parameter to modify.
   paramField.ParameterFieldName = paramName;

   // Set a value to the parameter.
   paramDiscreteValue.Value = paramValue;
   paramValues.Add (paramDiscreteValue);
   paramField.CurrentValues = paramValues;

   // Add the parameter to the ParameterFields collection.
   paramFields.Add (paramField);
   return paramFields;
}

Hi Wafaa. I looks like you added the param value, but did not add the new parameter to the field collection.

Hi David,
Yes,you are right i didn't .Thanks alot.But, the problem still persists.
What is the Specified argument was out of the range of valid values?.I don't get it.

What is the Specified argument was out of the range of valid values?.I don't get it.

The argument out of range exception is saying you are attempting to access an element outside of the allocated array size:

ParameterField parameterField = parameterFields[PARAMETER_FIELD_NAME];------  here i get the exeption

The definition PARAMETER_FIELD_NAME is a value that is larger/outside than the parameterFields.Count() - 1 element, which is the last possible element available.

Thanks alot david and adatapost appreciated.
Last question :-
Is step1 in adding parameter to the crystal report is:-
in .rpt file :-field explorer -->parameter field-->new ?.
I mean is that a mandatory step?.
Sorry for my stupid question.But when i tried to work with the previous code with a parameter i added to the report using the previous step it worked.

Is step1 in adding parameter to the crystal report is:-
in .rpt file :-field explorer -->parameter field-->new ?.
I mean is that a mandatory step?.

I don't know whether you can programattically add new parameter fields to a report without running into undefined/bad behavior. My question is, why would you want to add parameter fields dynamically? How can the report itself benefit from a field that otherwise is not referenced by the report?

And don't worry about "stupid questions"--I ask them all the time..;)

I don't know whether you can programattically add new parameter fields to a report without running into undefined/bad behavior. My question is, why would you want to add parameter fields dynamically? How can the report itself benefit from a field that otherwise is not referenced by the report?

I was just trying to make my client's dream become true.
But i handled it in a different way.Thanks DoubleD.:)

I was just trying to make my client's dream become true.
But i handled it in a different way.Thanks DoubleD.:)

Do you mind if I ask the situation and how you handled it?

Do you mind if I ask the situation and how you handled it?

Ofcourse not,do you remember my program you downloaded to see
know the problem about the crystal report.There are two tables the first table is a table of donors and the other is a table of voluntary contributions types. Donors donate to various kinds of voluntary contributions The table of voluntary contributions includes the total contributions for each type separately.Ok .he wanted to make the database divided into 50 records per page each page represents book of donations .He wanted to view the data of the 2 tables in the same page but according to the data in the page he chooses.
Actually,I'll try to do that using subreport.But I was afraid of data overlapping so i tried to make that using parameters but he would add new types of contributions.So i couldn't.

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.