locked
How to pass Parameter in Crystal report having two Sub report in C#.net and ASP.net RRS feed

  • Question

  • User1341726698 posted

    Hi All

       I created a report in Crystal Report and Bind with a storedprocedure which needs to parameter to executes. I want to pass it from Page_Load event of webpage to run and print report. Code is as bellow

     

    Following code not worked and prompting pass for parameters before executing report

    ----------------------------------------------------------------------------------------------------------------

    ReportDocument rptDoc = new ReportDocument();

    strReportPath = Server.MapPath(Session["REPORTNAME"].ToString());

     rptDoc.Load(strReportPath);

    rptDoc.SetParameterValue(0, Request.QueryString["Val1"].ToString());
     rptDoc.SetParameterValue(1, Request.QueryString["Val2"].ToString());
     rptDoc.Refresh();

    ConnectionInfo dbConnInfo = new ConnectionInfo();

                        dbConnInfo.ServerName = ".";
                        dbConnInfo.DatabaseName = "xxxxxx";
                        dbConnInfo.UserID = "sa";
                        dbConnInfo.Password = "xxxxxxx";

                        TableLogOnInfo tblLogOnInfo = new TableLogOnInfo();
                        Database db = rptDoc.Database;
                        Tables tbls = db.Tables;

                        foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in tbls)
                        {
                            tblLogOnInfo = tbl.LogOnInfo;
                            tblLogOnInfo.ConnectionInfo = dbConnInfo;
                            tbl.ApplyLogOnInfo(tblLogOnInfo);
                        }

          CV1.ReportSource = rptDoc;

         CV1.RefreshReport();

    ----------------------------------------------------------------------------------------------------------

    I also tried following code again runs without error but asking parameter pass page before executing that I dont want.

    --------------------------------------------------------------------------------------------------------------------------------------

    ParameterFields pfields = new ParameterFields();
                            ParameterField pfield = new ParameterField();
                            ParameterDiscreteValue disvalue = new ParameterDiscreteValue();

                            ParameterField pVoucherNo = new ParameterField();
                            ParameterDiscreteValue disVoucherNo = new ParameterDiscreteValue();

                            disvalue.Value = Request.QueryString["Val1"].ToString();
                            pfield.Name = "@VoucherID";
                            pfield.CurrentValues.Add(disvalue);
                            pfields.Add(pfield);

                            pVoucherNo.Name = "@VoucherNo";
                            disVoucherNo.Value = Request.QueryString["Val2"].ToString();
                            pVoucherNo.CurrentValues.Add(disVoucherNo);
                            pfields.Add(pVoucherNo);

                            CV1.ParameterFieldInfo = pfields;
                            CV1.ReportSource = strReportPath;

                        foreach (TableLogOnInfo cnInfo in CV1.LogOnInfo)
                        {
                            cnInfo.ConnectionInfo = dbConnInfo;
                        }
                       
                        CV1.ReportSource = rptDoc;
                       //Session["REPORTDOC"];
                       // CV1.SelectionFormula = Session["RECSELFORMULA"].ToString();
                        CV1.RefreshReport();

    Please help me to pass the parameter in crystal that It will not user to pass parameter.

    Thanks in Advance

    Monday, June 4, 2012 8:11 AM

All replies

  • User226262986 posted

    Hi

    Try the method which is applied in below code..

    Dim myCon As New SqlClient.SqlConnection()
    myCon.ConnectionString = ConfigurationManager.ConnectionStrings("MyCon").Co nnectionString
    Dim mycmd As New SqlClient.SqlCommand()
    Dim mycmd1 As New SqlClient.SqlCommand
    mycmd.Connection = myCon
    mycmd1.Connection = myCon
    ' For Stored Procedure
    mycmd.CommandType = CommandType.StoredProcedure
    mycmd.CommandText = "Depo_InvoicePrint"
    mycmd.Parameters.Add("@InvNo", SqlDbType.VarChar)
    mycmd.Parameters("@InvNo").Value = Session("InvoiceNo")
    
    mycmd1.CommandType = CommandType.StoredProcedure
    mycmd1.CommandText = "Depo_InvoicePrintSubreport"
    mycmd1.Parameters.Add("@InvNo", SqlDbType.VarChar)
    mycmd1.Parameters("@InvNo").Value = Session("InvNo")
    
    Dim myda As New SqlClient.SqlDataAdapter()
    myda.SelectCommand = mycmd
    Dim myDs As New DataSet()
    myda.Fill(myDs, "InvoicePrint") ' name of the Stored Proc for main report
    
    Dim myda1 As New SqlClient.SqlDataAdapter()
    myda1.SelectCommand = mycmd1
    Dim myDs1 As New DataSet()
    myda1.Fill(myDs1, "InvoicePrintSubreport") ' name of the Stored Proc for subreport
    
    mycmd.Dispose()
    mycmd1.Dispose()
    myCon.Close()
    
    oRpt.Load(Server.MapPath(c:/rptInvoicePrint.rpt"))
    oRpt.FileName = Server.MapPath(c:/rptInvoicePrint.rpt")
    oRpt.SetDataSource(myDs)
    oRpt.OpenSubreport("rptInvoicePrintSub").SetDataSo urce(myDs1)
    
    oRpt.SetParameterValue("@InvNo", Session("InvoiceNo")) 'parameter for main report
    oRpt.SetDatabaseLogon(Session("dbuser"), Session("dbpass"))
    
    oRpt.SetParameterValue("@InvNo", Session("InvoiceNo"), "rptInvoicePrintSub") ' parameter for subreport
    
    myCon.Close()

    for more details chack the following link

    http://bytes.com/topic/net/answers/760299-how-pass-parameter-sub-report-crystal-report-run-time

    Friday, August 23, 2013 5:07 AM
  • User844950252 posted

    Here is how a string parameter is pass..

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

    crParameterDiscreteValue.Value = textBox1.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["Customername"]; crParameterValues = crParameterFieldDefinition.CurrentValues;

    Full source code example.. ....... http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-string-parameter.htm

    Bona.

     

    Monday, March 31, 2014 2:50 AM