1

I am using Visual Studio 2012 , Window Form Application . I have created a report with Crystal report using stored procedure which accepts three parameters i-e @DateFrom, @DateTo, and @District, i have to pass these parameters from c# code.

I have the following code which accepts only one parameter not multiple, please help me

private void btn_Preview_Click(object sender, EventArgs e)
{
                string parm_From = "01-07-2014";
                string param_To = "30-06-2015";
                string param_District = "DistrictName";
                ReportDocument reportDocument = new ReportDocument();
                ParameterField paramField = new ParameterField();
                ParameterFields paramFields = new ParameterFields();
                ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

                paramField.CurrentValues.Add(parm_From);
                paramFields.Add(paramField);
                crystalReportViewer1.ParameterFieldInfo = paramFields;
                reportDocument.Load(@"\GenPensionReport.rpt");
                crystalReportViewer1.ReportSource = reportDocument;
                crystalReportViewer1.Refresh(); 
}

where i am going wrong?

2 Answers 2

1

After you load the report document, you just need to call:

reportDocument.SetParameterValue("DateFrom", dateFrom);
reportDocument.SetParameterValue("DateTo", dateTo);
reportDocument.SetParameterValue("District", district);

EDIT

// The names must match what Crystal expects, So if they contain @ you must include them.
reportDocument.SetParameterValue("@DateFrom", dateFrom);
reportDocument.SetParameterValue("@DateTo", dateTo);
reportDocument.SetParameterValue("@District", district);

This simply sets the parameter values to the report.

Sign up to request clarification or add additional context in comments.

5 Comments

These are the stored Procedure parameters, which is i am using in my report, these are "@DateFrom", "@DateTo" and "@District", after applying your method i got blank report.
Run the verify database menu from Crystal, and it should create these parameters for you, then whatever parameter names it creates, modify my code sample to match the parameters that the report now expects. Test it in the report designer preview to ensure it works.
These three parameters are already created with "@" symbol, and when i verify Database it ask me to provide these three parameters, after that it gives me accurate result, but through c# it gives me blank report.
Then you want "@DateFrom" and so on.
Thanks reckface , there was date and time format issue i-e mm/dd/yyy h:m:s , now it works
0
        CrystalReport1 objRpt = new CrystalReport1();
        objRpt.SetDataSource(ds);
        ParameterFields pfield = new ParameterFields();
        ParameterField ptitle = new ParameterField();
        ParameterField ptitle1 = new ParameterField();
        ParameterField ptitle2 = new ParameterField();
        ParameterDiscreteValue pvalue;
        ParameterDiscreteValue pvalue1;
        ParameterDiscreteValue pvalue2;
        ptitle.ParameterFieldName = "pdate";//pdate is a crystal report parameter name
        ptitle.CurrentValues.Clear();
        pvalue = new ParameterDiscreteValue();
        ptitle.CurrentValues.Add(pvalue);
        pvalue.Value = txtcolor.Text.ToString();

        pfield.Add(ptitle);

        ptitle1.ParameterFieldName = "no";//no is a crystal report parameter name
        ptitle1.CurrentValues.Clear();
        pvalue1 = new ParameterDiscreteValue();
        ptitle1.CurrentValues.Add(pvalue1);
        pvalue1.Value=txtno.Text.ToString();

        pfield.Add(ptitle1);

        ptitle2.ParameterFieldName = "date";//date is a crystal report       parameter name
        ptitle2.CurrentValues.Clear();
        pvalue2 = new ParameterDiscreteValue();
        ptitle2.CurrentValues.Add(pvalue2);
        pvalue2.Value = textBox1.Text.ToString();

        pfield.Add(ptitle2);

        crystalReportViewer1.ParameterFieldInfo = pfield;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.