locked
Passing Parameters to Subreports with stored procedure RRS feed

  • Question

  • Hi all,

    how can I pass a value from a main report to a stored procedure in subreport? 

    For better understanding:

    I have a main report which uses a stored procedure as a datasource. This stored procedure returns some values (fields) which I have to pass as a parameter to my subreports stored procedure. How can I achieve this?

    If there were no stored procedure, we could simply link fields from main report to subreport fields, and it worked fine, but now, something like this seems impossible.

    Can anyone help?
    Saturday, May 16, 2009 12:58 PM

Answers

  • Hi Maahir,

    How do you set the parameters to crystal report?
    Use this statement "rptDoc.SetParameterValue(paramname, paramvalue)", you can set parameters to the certain crystal report. Of course, all the settings are done dynamically. Here is the whole example for reference:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;
    
    public partial class T001_S0101_CrystalReportView : System.Web.UI.Page
    {
        #region 
        Hashtable parameters;
        ReportDocument rptDoc;
        string rptName;
        DataTable dt;
    
        #endregion
    
        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            FileOPS = new DiskFileDestinationOptions();
            ExOPS = new ExportOptions();
    
            parameters = new Hashtable();
            rptDoc = new ReportDocument();
            dt = new DataTable();
    
            parameters = (Hashtable)Session["parameters"];
            dt = (DataTable)Session["sourcetable"];
            rptName = (string)Session["rptName"];
    
            if (dt == null || dt.Rows.Count == 0)
            {
                Response.Redirect("DataNoFound.aspx");
                return;
            }
            else
            {
                rptDoc.Load(Server.MapPath(rptName));
                rptDoc.SetDataSource(dt);
    
                SetParameter();
    
                this.CrystalReportViewer1.ReportSource = rptDoc;
                this.CrystalReportViewer1.DataBind();
            }
        }
    #endregion
    
        
        private void SetParameter()
        {
            if (parameters != null)
            {
                foreach (string param in parameters.Keys)
                {
                    rptDoc.SetParameterValue(param, parameters[param]);
                }
            }
        }
    
        #endregion
    }

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Friday, May 22, 2009 1:15 AM
    Wednesday, May 20, 2009 7:26 AM