I found many tutorials on the internet that shows how to pass the parameters (Discreet Parameters) to crystal reports, I tried all of them, but unfortunately I didn’t get anyone of it to work. Therefore I though to give a trial on my code and eventually I got it running. I assume that you have a report ready with a parameter.
Firstly to get it running we have to use the below namespaces:
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
After you added a namespace now we have to pass a value to the report parameter, Here is the code to do this: Step 1: Create a ReportDocument object and initialize it
ReportDocument crReportDocument = new ReportDocument();
Step 2: Use the Load method of the ReportDocument object to load the report in the memory and use it.
Step 3: After loading the report in the ReportDocument object, we can now set the value to the report parameter using SetParameterValue method. This is an overloaded method which we can use according to the requirement. I have used the parameter name and parmeter value which are of string and object type respectively.
Step 4: As the data in the report is being populated from the database according to the parameter provided in the previous step. So to populate the data from the database we also have to set the database credentials to log in to the database and populate the report data.
crReportDocument.SetDatabaseLogon("sa", "pass#word1", "MX", "AdventureWorks");
Step 5: After the credentials have been set up for he database, the last step is to set the ReportSource property of the Crystal Report Viewer.
crystalReportViewer1.ReportSource = crReportDocument;
Here is the complete code:
ReportDocument crReportDocument = new ReportDocument(); crReportDocument.Load("C:\\report.rpt"); crReportDocument.SetParameterValue("id", 115); crReportDocument.SetDatabaseLogon("sa", "pass#word1", "MX", "AdventureWorks"); crystalReportViewer1.ReportSource = crReportDocument;