Print Page | Close Window

How to pass parameter to Subreport using C#?

Printed From: Crystal Reports Book
Category: Crystal Reports for Visual Studio 2005 and Newer
Forum Name: Data Connectivity
Forum Discription: How to connect to data sources and export reports
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=9742
Printed Date: 27 Apr 2024 at 12:35am


Topic: How to pass parameter to Subreport using C#?
Posted By: tahmeed
Subject: How to pass parameter to Subreport using C#?
Date Posted: 19 Apr 2010 at 7:16pm
Dear Experts,
Please help me to send parameter programetically to Crystal Report Subreport from C#. I can pass parameter to the main report using the following way:

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("rptMainReport.rpt");
ParameterFields parameterFields = new ParameterFields();
ParameterField parameterField = new ParameterField();
parameterField.ParameterFieldName = "myRoleID";
ParameterDiscreteValue parameterValue = new ParameterDiscreteValue();
parameterValue.Value = 2;
parameterField.CurrentValues.Add(parameterValue);
parameterFields.Add(parameterField);
CrystalReportViewer1.ParameterFieldInfo = parameterFields;
CrystalReportViewer1.ReportSource = cryRpt;


but having problem to send parameter to subreport. so please help me sending example code.

Thanx in advance


-------------
Tahmeed Ahmed



Replies:
Posted By: BrianBischof
Date Posted: 19 Apr 2010 at 7:46pm
Are you using .NET 2005/2008? If so, just do this:
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("rptMainReport.rpt");
cryRpt.SetParameterValue("myRoleId", 2, "subreport name");

and fill in the proper value for the subreport name.

I cover this in Chapter 16 of my CR.NET programming book.
http://www.amazon.com/exec/obidos/ASIN/097495361X/bischofsystem-20 - http://www.amazon.com/exec/obidos/ASIN/097495361X/bischofsystem-20


-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: tahmeed
Date Posted: 19 Apr 2010 at 8:44pm
Thanx BrianDischof.

-------------
Tahmeed Ahmed


Posted By: Lakshmi kanth
Date Posted: 02 Oct 2011 at 12:13am
Thanks a lot u saved my weekend


Posted By: NimaEntesari
Date Posted: 14 Dec 2012 at 6:13am
hello dears,
i use your code to pass parameter to subreport like code below:

SqlConnection cnn;
            string connectionString = null;
            string sql = null;

            connectionString = "data source=.;initial catalog=Lab2;Integrated Security=yes;";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = @"EXEC    [dbo].[pro_PrintReceptionReceptionInfo]" +
                            "@ReceptNum = '" + receptionNum + "'" +

                  @"EXEC    [dbo].[pro_PrintReceptionPatientTest]" +
                            "@ReceptionNum = '" + receptionNum + "'" +

                  @"EXEC    [dbo].[pro_PrintReceptionPatientInfo]" +
                            "@PatientNum = '" + patientNum + "'" +

                  @"EXEC    [dbo].[pro_PrintReceptionDepositFund]" +
                            "@ReceptNum = '" + receptionNum + "'";

            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            dscmd.Fill(ds, "ReceptionInfo, PatientTest, PatientInfo, DepositFund");
            cnn.Close();

            ParameterFields pFields = new ParameterFields();

            ParameterField pf = new ParameterField();
            ParameterDiscreteValue pdv = new ParameterDiscreteValue();
            pdv.Value = int.Parse(receptionNum);
            pf.Name = "@ReceptNum";
            pf.CurrentValues.Add(pdv);

            ParameterField pf2 = new ParameterField();
            ParameterDiscreteValue pdv2 = new ParameterDiscreteValue();
            pdv2.Value = int.Parse(receptionNum);
            pf2.Name = "@ReceptionNum";
            pf2.CurrentValues.Add(pdv2);

            ParameterField pf3 = new ParameterField();
            ParameterDiscreteValue pdv3 = new ParameterDiscreteValue();
            pdv3.Value = Int64.Parse(patientNum);
            pf3.Name = "@PatientNum";
            pf3.CurrentValues.Add(pdv3);
           
            ParameterField pf4 = new ParameterField();
            ParameterDiscreteValue pdv4 = new ParameterDiscreteValue();
            pdv4.Value = int.Parse(receptionNum);
            pf4.Name = "@ReceptNum";
            pf4.CurrentValues.Add(pdv4);

            pFields.Add(pf);
            pFields.Add(pf2);
            pFields.Add(pf3);
            pFields.Add(pf4);

            crystalReportViewer1.ParameterFieldInfo = pFields;

            ReceptionPrint objRpt = new ReceptionPrint();
            objRpt.SetDataSource(ds);
            objRpt.SetParameterValue("@ReceptionNum", int.Parse(receptionNum), "ReceptionPrintTest");
            objRpt.SetParameterValue("@ReceptNum", int.Parse(receptionNum), "DepositPrintSubReport");

            crystalReportViewer1.ReportSource = objRpt;

but when report loads, it send a message that say "the parameter is incorrect

thanks alot
Smile


Posted By: NimaEntesari
Date Posted: 16 Dec 2012 at 12:39pm
Please help me about this, please :(



Print Page | Close Window