NipunR
NipunR

Reputation: 61

Steps to Pass parameters to crystal reports in C#

Can you tell me what are the steps to pass parameters to crystal reports 13 in C# win form..

my code:

        //getting and set dataset to report   
        string sql = "select * from dbo.Trading_Order";
        DataRetriever dr = new DataRetriever();
        dr.getValueFromCustomer(sql);
        DataTable dtSum = dr.getDataTable();
        dsMyReprt k = new dsMyReprt();
        k.Tables.Remove("dtMyTable");
        dtSum.TableName = "dtMyTable";
        k.Tables.Add(dtSum);
        CrystalReport1 myDataReport = new CrystalReport1();

        //pass parameter

        ParameterFields paramFields = new ParameterFields();
        // ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

        ParameterField paramField = new ParameterField();
        ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
        paramField.Name = "@DTotal";
        paramDiscreteValue.Value = tot;
        paramField.CurrentValues.Add(paramDiscreteValue);
        paramFields.Add(paramField);

        paramField = new ParameterField(); 
        paramDiscreteValue = new ParameterDiscreteValue(); 
        paramField.Name = "@name";
        paramDiscreteValue.Value = name;
        paramField.CurrentValues.Add(paramDiscreteValue);
        paramFields.Add(paramField);

        crystalReportViewer1.ParameterFieldInfo = paramFields;

        myDataReport.SetDataSource(k);
        crystalReportViewer1.ReportSource = myDataReport;

getting and set dataset part is working but passing parameters part is not working

Upvotes: 4

Views: 44500

Answers (3)

Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1926

You probably have figured the solution by now. But this may help. You enter values that are passed as a parameter.

http://www.codeproject.com/Tips/753879/Automatically-Setting-a-Parameter-from-a-Csharp-Va

Upvotes: 2

Gabriel L.
Gabriel L.

Reputation: 5014

I got big headaches with that for weeks... I have to precise that I set a sql query in the Crystal Reports Designer. Thus, I didn't use a dataTable like you did, so you have to consider that.

Well, @campagnolo_1 suggested you :

ReportDocument myDataReport = new ReportDocument();
myDataReport.Load(@"C:\Reports\Report.rpt");

myDataReport.SetParameterValue("MyParameter1", "Hello1");
myDataReport.SetParameterValue("MyParameter2", "Hello2");
myDataReport.SetParameterValue("MyParameter3", "Hello3");

This is the short and sweet solution. But, following this, you have to make sure you have created MyParameter1, MyParameter2 and MyParameter3of type String in the Crystal Reports Designer.

  1. It's important to mention that you have to Load the report before setting your parameters with SetParameterValue.

  2. If your parameter's name is MyParameter1, then don't add a @ in front like this :

    myDataReport.SetParameterValue("@MyParameter1", "Hello1"); // Your program will crash.

  3. If you got The parameter is incorrect then you should make sure the type of parameter value you gave is exactly the same as the parameter type. For example, if you have a parameter StartDate as type Date, then make sure the value you'll give is of type Date and has the right date format.

Also, you have talked about dynamic or static field. In your case, I think you enter values manually, then this is static field.

Hope this helps you.

Upvotes: 8

campagnolo_1
campagnolo_1

Reputation: 2750

Why not try it this way and save yourself some coding?

ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myDataReport.Load(@"C:\Reports\Report.rpt");

myDataReport.SetParameterValue("MyParameter1", "Hello1");
myDataReport.SetParameterValue("MyParameter2", "Hello2");
myDataReport.SetParameterValue("MyParameter3", "Hello3");

Upvotes: 0

Related Questions