简体   繁体   English

在C#中将参数传递给水晶报表的步骤

[英]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.. 你能告诉我在C#win形式中将参数传递给crystal Reports 13的步骤是什么?

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 获取和设置数据集部分正在工作但传递参数部分不起作用

I got big headaches with that for weeks... I have to precise that I set a sql query in the Crystal Reports Designer. 几周以来我一直很头疼...我必须确切地说我在Crystal Reports Designer中设置了一个sql查询。 Thus, I didn't use a dataTable like you did, so you have to consider that. 因此,我没有像你那样使用dataTable,所以你必须考虑这一点。

Well, @campagnolo_1 suggested you : 那么,@ campagnolo_1建议你:

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 MyParameter3 of type String in the Crystal Reports Designer. 但是,在此之后,您必须确保在Crystal Reports设计器中创建了String类型的MyParameter1MyParameter2MyParameter3

  1. It's important to mention that you have to Load the report before setting your parameters with SetParameterValue . 重要的是要提到在使用SetParameterValue设置参数之前必须加载报告。

  2. If your parameter's name is MyParameter1 , then don't add a @ in front like this : 如果参数的名称是MyParameter1 ,那么不要像这样在前面添加@

    myDataReport.SetParameterValue("@MyParameter1", "Hello1"); 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. 如果你得到The parameter is incorrect那么你应该确保你给出的参数值的类型与参数类型完全相同。 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. 例如,如果您将参数StartDate作为Date类型,那么请确保您将给出的值为Date类型并具有正确的日期格式。

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. 希望这对你有所帮助。

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 http://www.codeproject.com/Tips/753879/Automatically-Setting-a-Parameter-from-a-Csharp-Va

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM