Passing parameters to crystal reports in C#

how to pass parameter in crystal report in c# windows application
how to pass parameter in crystal report in c# web application
pass multiple parameters to crystal reports c#
how to pass date parameter in crystal report in c#
pass textbox value to crystal report c#
how to pass value to formula field in crystal report using c#
how to print textbox value in crystal report
how to set parameter value in crystal report

I've been trying to get this to work for a while, and all the example code I've seen aren't quite doing what I'm doing.

I have a program that returns a pdf of a report that I pass a data table to. This works fine, except I would like to pass it a couple of other parameters (the date range of the table, stats etc) and I just can't get it to work. My code basically looks like this.

ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myDataReport.Load(@"C:\Layouts\Report.rpt");
ParameterField myParam = new ParameterField();
ParameterDiscreteValue myDiscreteValue = new ParameterDiscreteValue();
myParam.ParameterFieldName = "MyParameter";
myDiscreteValue.Value = "Hello";
myParam.CurrentValues.Add(myDiscreteValue);
myDataReport.ParameterFields.Add(myParam);
myDataReport.SetDataSource(myDataTable);
Stream returnData = myDataReport.ExportToStream(PortableDocFormat);
myDataReport.Close();
return returnData;

I have added the parameter field in the rpt document in crystal, do I have to change anything in the xsd file in c#, or am I missing something completely different?

Many thanks, Andy.

All that parameter code can be replaced with...

// Set datasource first
myDataReport.SetDataSource(...)
// Assign Paramters after set datasource
myDataReport.SetParameterValue("MyParameter", "Hello");

I can't remember if the order matters when setting the datasource and parameters. Maybe try setting the datasource first. The xsd/datasource has no relation to crystal parameters.

UPDATE1

SetParameterValue AFTER the datasource asignation or you will recieve error "Missing parameter values."

Passing parameters to crystal reports in C#, All that parameter code can be replaced with // Set datasource first myDataReport.SetDataSource() // Assign Paramters after set datasource  What about passing a parameter as a parameter within Crystal Report from C#? I'm encountering a lot of problem at my end. I'm unsure if my C# code is incorrect or the Crystal Report I created is incorrect.

ReportDocument cryRpt = new ReportDocument();

TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;

string path = "C:/reportpath/report.rpt";
cryRpt.Load(path);

cryRpt.SetParameterValue("MyDate2", str2);
cryRpt.SetParameterValue("MyDate", str1);

crConnectionInfo.ServerName = "server";
crConnectionInfo.DatabaseName = "DataBase";
crConnectionInfo.UserID = "user";
crConnectionInfo.Password = "password";

CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
    crtableLogoninfo = CrTable.LogOnInfo;
    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
    CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh(); 

Steps to Pass parameters to crystal reports in C#, 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  In one of my previous article, I have shown with example on how to add a Crystal Report using XML data in Asp.Net.Taking a step further, now, in this article, I will demonstrate with an example on how to pass parameters to a Crystal Report dynamically, using Asp.Net.

       //create object of crystal report.
        CrystalReport1 objRpt = new CrystalReport1();
        objRpt.SetDataSource(ds);
        ParameterFields pfield = new ParameterFields();
        ParameterField ptitle = new ParameterField();
        ParameterDiscreteValue pvalue = new ParameterDiscreteValue();
        ptitle.ParameterFieldName = "date";
        pvalue.Value = txtcolor.Text;
        ptitle.CurrentValues.Add(pvalue);
        pfield.Add(ptitle);
        crystalReportViewer1.ParameterFieldInfo = pfield;
        crystalReportViewer1.ReportSource = objRpt;
        crystalReportViewer1.Refresh();

C# Crystal Reports String parameter, how to pass a String parameter to Crystal Reports from C# application. I am new to crystal report.I have designed the crystal report by following this link Crystal Report with SQL Stored Procedure Parameter and Visual Studio Actually i need to pass different ID(Input value of the SP) to the SP that i connected with the Crystal report. This is the Code that i am Passing the ID to crystal report :

                rptTeacherTimeTable ttReport = new rptTeacherTimeTable();
                DataTable dt = new DataTable();
                dt = ObjclsT_TimeTable.GetTimeTableByClass(ClassID);

                objReport = ttReport;
                objReport.SetDataSource(dt);
                objReport.SetParameterValue("RTitle", "Class Time Table");
                objReport.SetParameterValue("STitle", "Teacher Time Table");
                reportViewer.crystalReportViewer1.ReportSource = objReport;
                reportViewer.crystalReportViewer1.Show();
                reportViewer.Show();

How to pass value in crystal report C#, Without pointing out the number of examples and tutorials that are available.. I typically create a Report against the database I don't use  I'm working on Visual Studio 2008 and SQL Server 2008, language C#. I want to pass multiple parameters to Crystal Report in ASP.NET. I have two parameters @accountnumber and @customerid. But I can only pass one parameter to my report as in code below.

just do one thing and your code will run automatically set the data source first before adding parameters...

 crp rpt = new crp();
 rpt.SetDataSource(dt);
 rpt.SetParameterValue("p",p.ToString());

How to pass parameter to a crystal report using c# - P2P Wrox, I presume you are using a crystal reports viewer from within a c# form. Anyway, this is how I passed a formula field so I guess parameter fields  In the new parameter section of the crystal report, give the name of the parameter and click OK. Here I gave a designation as a parameter name. After that take Record selection formula from the special field in the crystal report and then right-click on the Record selection formula and Select Expert à Record.

Passing parameters to Crystal Reports at runtime with c#, This small article shows how you can create and pass parameters value at runtime in a Crystal Report from your UI. In my application, I am  I have a asp.net web application that will contain one crystal report there i have defiend two selection criteria and also have two parameter for that selection criteria like FirstName and second is LastName now i want to know how to pass that parameter value to my cristal report using c# I m using SAP CrystalReport for VSD2010 (CR13)

Passing parameter to Crystal Report in Asp.Net C# and Vb.Net, This blog explains you how to pass parameters to crystal reports at runtime while using c#. Here we pass a String parameter from C# to Crystal Reports. For example, from C# program we pass a customer name as a parameter and show all the orders of that customer to the Crystal Reports. In the previous tutorial we saw how to generate a C# - Crystal Reports from multiple tables.

how to pass 3 parameters in crystal report c# .net, This is an article we will explain how to pass single or multiple parameters to a crystal report dynamically using asp.net. For C# and Vb.Net developers. I typically create a Report against the database I don't use datasets. I also typically use Stored Procedures for Reports - they are quicker and have distinct advantages when you have sub-reports. the following link will show you how to use datasets though; Code Project - Creating Crystal Reports using C# with Datasets

Comments
  • Yep that was it! I think I tried that line before, but I had the datasource set in the wrong place as you pointed out. Works compeletly now, thanks!
  • @Andrew. Could you please tell me where did you put the call to the dataSource?
  • Put SetParameterValue AFTER the datasource asignation
  • Please explain what you have fixed or are suggesting. Just adding code is not much helpful.
  • Please explain!