Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > Crystal Reports
Crystal Reports General discussion about Crystal Reports. For discussions specific to the book Professional Crystal Reports for VS.NET, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Crystal Reports section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 13th, 2006, 06:53 AM
Registered User
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shriji.1111
Default Crystal Report Parameter pass from c#

Dear Friends,
How to pass parameters in c# to Crystal Report.
I already create Crystal Reprot using store Procedure.
But How can i pass parameters to that reoprts.
My Code: -
Add Librarioes: -
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
sqlcmd.CommandText = "Sp_Report1";
sqlcmd.CommandType = CommandType.StoredProcedure;

sqlcmd.Connection = con;

string path;
ReportDocument orpt = new ReportDocument();

path = Request.PhysicalApplicationPath;
path = path + "SellPro.rpt";

            CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions ();

            orpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableD ocFormat;
            orpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.Disk File;

path = Request.PhysicalApplicationPath;
diskOpts.DiskFileName = path + "1.pdf";
orpt.ExportOptions.DestinationOptions = diskOpts;


************************************************** ***
What i do?

Brijesh Shah

Brijesh Shah
Old October 13th, 2006, 11:16 AM
Registered User
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

Hi, I've got some .NET 2003 C# code that may help you. The code sample below extracts an HTML form value passed to the aspx page, then assigns it as a Parameter Field (PF) which is passed to my sprocs, plus assigns ADO connection info to the report datasources. (My report is actually a master report with a bunch of subreports that produce a single PDF.) The main parameter, @strSprocParameter, is what gets passed into the sprocs. You can use add'l params the same way, like my bSubReportX params. In my case, the same param is passed to all my sprocs. You can set up subreport parameter fields in exactly the same way, linking the strParameter param to each subreport's param. I believe you can set up more than 1 param, but I didn't need to. Another quick-n-dirty method is to pass a string that you can break up into an array of params in your sproc, but that's kind of kludgy. :)

using CrystalDecisions;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;

private void Page_Load(object sender, System.EventArgs e)
    // Put user code to initialize the page here
    int i, j;
    CrystalDecisions.CrystalReports.Engine.ReportDocum ent objReport
        = new CrystalDecisions.CrystalReports.Engine.ReportDocum ent();
    CrystalDecisions.Shared.ConnectionInfo conn = new ConnectionInfo();
    CrystalDecisions.CrystalReports.Engine.Table myTable;
    CrystalDecisions.Shared.TableLogOnInfo myLog;
    System.IO.MemoryStream ios;
    ios = new System.IO.MemoryStream();

    // retrieve HTML form fields passed to the aspx page
    string strParameter = Request.Form.Get("txtParameter");
    string strReportPath = Request.Form.Get("txtReportPath");

    // which subreports are to be generated? each is an HTML checkbox
    bool bSubReportA = Request.Form.Get("chkSubReportA") == "on" ? true : false;
    bool bSubReportB = Request.Form.Get("chkSubReportB") == "on" ? true : false;
    bool bSubReportC = Request.Form.Get("chkSubReportC") == "on" ? true : false;

    // load the report and set its runtime parameters
    // Note: each needs to be added to the report as a Parameter Field, and the
    objReport.Load(strReportPath);//, OpenReportMethod.OpenReportByTempCopy);
    objReport.Refresh(); // needed so report will accept new parameters
    objReport.SetParameterValue("@strSprocParameter", strParameter);
    objReport.SetParameterValue("bSubReportA", bSubReportA);
    objReport.SetParameterValue("bSubReportB", bSubReportB);
    objReport.SetParameterValue("bSubReportC", bSubReportC);

    // assign the same database connection for each report
    conn.ServerName = "My_System_DSN";
    conn.DatabaseName = "MY_Database";
    conn.UserID = "SomeUserID";
    conn.Password = "SomePassword";
    for (i=0; i<objReport.Database.Tables.Count; i++) {
        myTable = objReport.Database.Tables[i];
        myLog = myTable.LogOnInfo;
        myLog.ConnectionInfo = conn;
        myTable.Location = myLog.TableName;

    // execute the report and export to a PDF memory buffer
    ios = (MemoryStream)objReport.ExportToStream(CrystalDeci sions.Shared.ExportFormatType.PortableDocFormat);
    Response.Buffer= true;
    Response.ContentType = "application/pdf";

I hope that helps. The 1st parameter is the std type set up according to the directions in the CR help docs built into both the MSDN help and CR's Report Designer (the standalone design software). That's why you see the "@" in its name. The others are extra parameters I added so I can include/exclude any of the subreports.
Old October 16th, 2006, 03:55 AM
Registered User
Join Date: Oct 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shriji.1111

Thanks for Reply.

Brijesh Shah
Old April 14th, 2009, 06:44 AM
Posts: n/a
Default C# crystal report parameter

ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

crParameterDiscreteValue.Value = "Your Value"
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["Your Field"];
crParameterValues = crParameterFieldDefinition.CurrentValues;

crParameterFieldDefinition.ApplyCurrentValues(crPa rameterValues);


Old June 14th, 2011, 11:38 AM
Registered User
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks so much hu_yang, your code saved my life!

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass parameter to a crystal report using c# preetakrish BOOK: Professional Crystal Reports for VS.NET 16 November 19th, 2009 05:26 AM
pass parameter in crystal report srivigdev C# 3 April 14th, 2008 12:11 PM
How to pass parameter to sub-report . azeerum Access 1 January 3rd, 2005 04:26 PM
How to Pass parameter to chart in crystal Report. simsimlhr C# 3 April 12th, 2004 11:04 AM
Pass parameter to store procedure & Crystal Report quinn Classic ASP Professional 0 March 10th, 2004 02:16 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.