locked
how to pass parameter to crystal report in asp.net RRS feed

  • Question

  • User-1078128378 posted

    hi all,

    i am using asp.net 2010 and crystal report 13

    i am using crysyal report command in crystal reporta

    that command contains parameter in where condition

    i want to pass the value to the parameter from my program

    but when i run the report it prompts the value, but i need to pass the value from my textbox control

    please slove this issue.

    Thanks,

    Murali.

    Friday, May 31, 2013 5:32 AM

All replies

  • User1953918952 posted

    actually its very simple matter. avoid all that parameter code, just use something like following:

    ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    myDataReport.Load(@"C:\Layouts\Report.rpt");

    myDataReport.SetParameterValue("MyParameter1", "Hello1");
    myDataReport.SetParameterValue("MyParameter2", "Hello2");
    myDataReport.SetParameterValue("MyParameter3", "Hello3");

    Friday, May 31, 2013 5:36 AM
  • User-1078128378 posted

    hi aftab i wrote ur above code

    but it says MISSING PARAMETER VALUES

    my code is:

      crystalReportViewer1.RefreshReport();
                 ReportDocument rptdoc = new ReportDocument();
                rptdoc.Load(Server.MapPath(reportpath));
                rptdoc.SetParameterValue("FromDate", Convert.ToDateTime(txtFromDate.Text).ToShortDateString());
                rptdoc.SetParameterValue("ToDate", Convert.ToDateTime(txtToDate.Text).ToShortDateString());
              
                crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ReportSource = rptdoc;
    
    Friday, May 31, 2013 5:47 AM
  • User1953918952 posted

    I think you need to call GenerateReport(); method in page load as well try following

     protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
            {

                GenerateReport();
                ViewState["ReportLoad"] = "Load";
            }

      protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["ReportLoad"] != null)
                {
                    GenerateReport();
                }
            }

    Friday, May 31, 2013 6:50 AM
  • User1953918952 posted

    Try to pass the parameters like this

    CrystalReportSource1.ReportDocument.SetParameterValue(0, "ParameterValue");

    Friday, May 31, 2013 6:59 AM
  • User-1078128378 posted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CrystalDecisions.Web;
    using System.Data;
    using System.Data.SqlClient;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;
    
    public partial class ReportDayWise : System.Web.UI.Page
    {
        ReportDocument rptdoc;
        
        protected void Page_Init(object sender, EventArgs e)
        {
            if (rptdoc == null)
            {
                rptdoc = new ReportDocument();
            }
            if (!IsPostBack)
            {
                Session["ReportName"] = null;
                crystalReportViewer1.ReportSource = null;
                crystalReportViewer1.RefreshReport();
                crystalReportViewer1.Visible = false;
                        }
            else
            {
                loadReport();
            }
           
            // Do any related intialization work.
        }
        public void loadReport()
        {
            if(Session["ReportName"] != null)
            {
                string Report = Session["ReportName"].ToString();
                         rptdoc.Load(Server.MapPath(Report));
                         crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ReportSource = rptdoc;
                          crystalReportViewer1.Visible = true;
            
            }
        }
        protected void Page_Unload(object sender, EventArgs e)
        {
            rptdoc.Close();
            rptdoc.Dispose();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
                    
        }
    
        protected void btnDayWiseDayRegister_Click(object sender, EventArgs e)
        {
          loadReport("~/AllReports/OP/DayWiseReport.rpt");
          //  loadReport("~/AllReports/DayConsumption.rpt");
            
        }
    
        public void loadReport(string reportpath)
        {
                     crystalReportViewer1.RefreshReport();
                rptdoc.Load(Server.MapPath(reportpath));
                rptdoc.SetParameterValue(0, Convert.ToDateTime(txtFromDate.Text).ToShortDateString());
                rptdoc.SetParameterValue(1, Convert.ToDateTime(txtToDate.Text).ToShortDateString());
                crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ReportSource = rptdoc;
             }
        
    }


    hi aftab it is not working i am getting error.

    i am using above code in aspx.cs file

    and i also unchecked Enable Report parameter prompting

    when i ran the applicatioon it says

    Load Report Failed

    Saturday, June 1, 2013 3:27 AM
  • User-1907569630 posted

    if u have class with defined properties,

    please use 

    1)passing parameter in dataset =ds. i.e get details & put to ds.

    2)using System.Reflection;

    using CrystalDecisions.CrystalReports.Engine;

    3)ReportDocument doc = new ReportDocument();

    doc.Load(AppDomain.CurrentDomain.BaseDirectory + objIni.GetKeyFieldValue("SQL", "initial catalog") + @"\" + rep_nm + ".rpt");
    doc.SetDataSource(ds.Tables[0]);

    Type t = typeof(Class_Name);
    PropertyInfo[] publicFieldInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);

    ParameterFieldDefinitions crParameterdef;
    crParameterdef = doc.DataDefinition.ParameterFields;
    foreach (PropertyInfo field in publicFieldInfos)
    {
    foreach (ParameterFieldDefinition def in crParameterdef)
    {
    if (null != field)
    {
    if (def.Name.Equals(field.Name.ToLower())) // check if parameter exists in report
    {
    doc.SetParameterValue(field.Name.ToLower(), field.GetValue(Class_Instance_Name, null) == null ? string.Empty : field.GetValue(Class_Instance_Name, null)); // set the parameter value in the report
    }
    }
    }
    }
    crystalReportViewer1.ReportSource = doc;

    Saturday, June 1, 2013 3:39 AM
  • User-1078128378 posted

    Hi Sharanamma,

    still it says Load Report Failed

    and also i tried following code

    ReportDocument rptdoc = new ReportDocument(); 
    string reportpath="DayWiseReport.rpt";
    rptdoc.Load(Server.MapPath(reportpath));    
            ParameterField pf = rptdoc.ParameterFields["FromDate"];
    
            ParameterDiscreteValue pd1;
            pd1 = new ParameterDiscreteValue();
            pd1.Value = Convert.ToDateTime(txtFromDate.Text);
            pf.CurrentValues.Add(pd1);
            ParameterField pf1 = rptdoc.ParameterFields["ToDate"];
    
            ParameterDiscreteValue pd2;
            pd2 = new ParameterDiscreteValue();
            pd2.Value = Convert.ToDateTime(txtToDate.Text);
            pf1.CurrentValues.Add(pd2);
    
          //  rptdoc.SetParameterValue(0, pf.CurrentValues);
           // rptdoc.SetParameterValue(1, pf1.CurrentValues);
    
            
                crystalReportViewer1.RefreshReport();                        
                crystalReportViewer1.ReportSource = rptdoc;
                crystalReportViewer1.RefreshReport();   
                crystalReportViewer1.Visible = true;

    but it still it says load report failed

    FYI

    i am using SAP crystal report 13 for visual studio 2010

    and i am using crystal report command in reports.

    please slove my issue...

    Thanks,

    Murali.

    Saturday, June 1, 2013 1:36 PM
  • User-578610739 posted

    Hi please check ,

    http://www.codeproject.com/Questions/265212/how-to-set-parameter-to-crystal-report-in-code

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/0f668cd3-24e1-4e6f-aadc-2fe1a1a995ee

    ReportDocument report = new ReportDocument();

    report.Load(Server.MapPath(@"~/reports/rptCustomerRecords.rpt"));

    report.FileName = Server.MapPath(@"~/reports/rptCustomerRecords.rpt");

    report.SetParameterValue("@Userid", userid); //  @UserId is the parameter you added at the design of the crystalReport and userid is the value for the parameters

    CrystalReportViewer1.ReportSource = report;

    refer link. http://forums.asp.net/t/1432158.aspx

    Monday, June 3, 2013 1:00 AM
  • User-1078128378 posted

    Hi Ajay,

    Thanks for ur reply

    i am passing two parameters those are date parameters

    string reportpath = Session["ReportName"].ToString();
    
                rptdoc.Load(Server.MapPath(reportpath));
                rptdoc.FileName = Server.MapPath(reportpath);
                DateTime FromDate = Convert.ToDateTime(txtFromDate.Text);
                DateTime ToDate = Convert.ToDateTime(txtToDate.Text);
                rptdoc.SetParameterValue("@FromDate", FromDate);
                rptdoc.SetParameterValue("@ToDate", ToDate);
                crystalReportViewer1.ReportSource = rptdoc;
                crystalReportViewer1.Visible = true;


    i am getting following exception.

    Server Error in '/OHC' Application.


    Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Runtime.InteropServices.COMException: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

    Monday, June 3, 2013 6:30 AM
  • User-1078128378 posted

    Thanks Ajay

    now my problem is sloved

    and i have one more doubt

    now i am developing in developement server now it every thing works fine

    when i move this app to production server will i get any errors?

    is there any chance to cause errors(which type of errors) while deploying...?

    Tuesday, June 4, 2013 2:23 AM
  • User-578610739 posted

    Hi Murali,

    No, never get error.

    If same environment the app will get.

    Generally crystal report runtime version will made issue, Other wise no issue will occur.

    Yes one more thing I would like to inform that never give any hardcore value in path or passing the parameter as well. Web.config is very usefull for this. It is the place where you done your hardcore.

    Tuesday, June 4, 2013 4:07 AM
  • User-1078128378 posted

    Hi Ajay,

    Thanks for ur suggestion.

    but i am new to crystal report i cant understand what u have said

    I would like to inform that never give any hardcore value in path or passing the parameter as well. Web.config is very usefull for this. It is the place where you done your hardcore.

    could you please give me an example for the above statement

    then it will helpful to me.

    Thanks,

    Murali.

    Tuesday, June 11, 2013 12:44 AM
  • User-578610739 posted

    Hi Murali,

    Hardcore value means do not give value which you can not change after deploy which you want change in future or frequently like define connection string in individual class , hardcore path for file( c:\myproject\report\ ) or any hardcore value which you want to vary on an codition ( int i = 20 //where 20 is total count ). or any webservice url like that.

    good to read web.config.(http://msdn.microsoft.com/en-us/library/aa306178.aspx)

    Wednesday, June 12, 2013 1:14 AM