Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to CRYSTAL REPORTS via C# in asp.net

I am new to Crystal Reports. I have designed a Crystal Report by following this link: Crystal Report with SQL Stored Procedure Parameter and Visual Studio What I need to do is pass different ID's (Input value of the SP) to the SP that I connected with the Crystal Report.

This is the code I have which passes the ID to the Crystal Report :

        protected void Button1_Click(object sender, EventArgs e)
        {
        string QuotationID = ViewState["QUOTATION_ID"].ToString();
        ReportDocument reportDocument = new ReportDocument();
        ParameterField paramField = new ParameterField();
        ParameterFields paramFields = new ParameterFields();
        ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();


       
        paramField.Name = "@id";


        paramDiscreteValue.Value = QuotationID;

        paramField.CurrentValues.Add(paramDiscreteValue);
        paramFields.Add(paramField);

        
        paramFields.Add(paramField);

        CrystalReportViewer1.ParameterFieldInfo = paramFields;

        string reportPath = Server.MapPath("~/CrystalReport.rpt");

        reportDocument.Load(reportPath);

       
        CrystalReportViewer1.ReportSource = reportDocument;
        }

But whenever I click the button it asks for the ID...enter image description here

like image 388
user2536023 Avatar asked Nov 07 '13 14:11

user2536023


People also ask

How do you pass parameter fields in Crystal Report?

You can pass parameter values to a crystal report programmatically using ReportDocument. DataDefinition. ParameterFields member, which represents a collection of parameters associated with a report. Before you use ParameterFields, you must import CrystalReport.

How do you pass multiple custom values to a parameter in Crystal Reports?

Go to Report menu>> Select Expert >> Record. Select the field for which the filter is to be applied. Click on OK. Now, refresh the report and enter multiple values seperated by comma(,).

When parameters should be used in Crystal Reports?

Parameters are used to take user inputs before the report is generated. User has to answer the prompt before the report is generated and the report output depends on the response of the user for the parameter value.


1 Answers

To set parameter on crystal I always do it this way:

ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(reportPath);
reportDocument.SetParameterValue("@id", QuotationID);

if you want to convert your report to a pdf:

var exportOptions = reportDocument.ExportOptions;
exportOptions.ExportDestinationType = ExportDestinationType.NoDestination;
exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
var req = new ExportRequestContext {ExportInfo = exportOptions};
var stream = reportDocument.FormatEngine.ExportToStream(req);

this returns you back a filestream that you can open from the aspx page.

like image 169
Raphael Avatar answered Nov 14 '22 23:11

Raphael