Passing Parameters to Crystal Reports

by Prashant 21. October 2009 01:26

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.

crReportDocument.Load("C:\\report.rpt");

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.

crReportDocument.SetParameterValue("id", 115);

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;
Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: ,

C#

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Visit blogadda.com to discover Indian blogs

About

Name of authorMy name is Prashant Khandelwal. I am a .NET programmer and technology enthusiast from New Delhi, India.

       

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010

Creative Commons License