Steps to Pass parameters to crystal reports in C#

不羁岁月 提交于 2019-12-19 09:06:25

问题


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


回答1:


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.




回答2:


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




回答3:


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");


来源:https://stackoverflow.com/questions/19930121/steps-to-pass-parameters-to-crystal-reports-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!