-->

14 March 2020

Parameter in Crystal Report in Asp.Net

  Asp.Net CS By Example       14 March 2020
 Parameter in Crystal Report in Asp.Net 
 In this post Parameter in Crystal Report, we shall see on how to create a Parameter Fields in Crystal Reports, and display the results in an Asp.Net page using web Browser.
 Parameter is an important part of a report. Using the parameter we can design a dynamic report. In this post, we take a closer look at using parameters in our reports, as well as how parameter fields can be created and implemented.If you don't kwon how to add crystal-reports and link dataset to crystal-reports to known more Click Here.

Creating parameter fields in reports includes two distinct steps:
   1) Creation of the parameter field.
   2) Implementation of the parameter field into the report.

How to Parameter Fields In crystal Report:

=> First Goto 'Field Explorer'.
  => Select 'Parameter Fields'.
  => Right Click on it & select 'New'.
Field Explorer.png

  => Now 'Create New Parameter' Screen will be open.
  => After Giving Parameter Field Parameter Name and selecting DataType Press on 'OK' Button.
Create New Parameter.png

   Each of the following input properties is presented within the Create/Edit Parameter Field dialog, shown in above figure:
Sr.No Properties Description
1) Name A meaningful name to the parameter field.
2) Prompting Text A statement or question presented to the business user within the report prompt dialog for the parameter field.
3) Value Type A list of available field types that correspond to how we want to use the parameter field within the report, including String (the default option), Boolean, Currency, Date, Date Time, Number, and Time.
4) Allow Multiple Values Enables the user to enter more than a single value for the parameter field.
5) Discrete Value(s) Enables the user to enter only a single value for the parameter field.
6) Range Value(s) Enables the user to specify a range, using start and end values, for the parameter field.
7) Discrete and Range Values Enables the user to specify both Discrete and range values, for the parameter field.
8) Default Values An option that allows the report designer to set a default value for the static list of values. When we select or click on Default values button the following window will be appeared.
9) Browse table and field Here we can select database table as well as corresponding fields as default values.
10) Length limit we cans set the minimum and maximum length limits for the parameter field.
11) Edit Mask Used to enter an Edit Mask for string data types rather than specifying a range of values. The Edit Mask can be any of a set of masking characters(such as A, a, 0, 9, #, L, &, C, , \ etc.) used to restrict the values we can enter as parameter values.
12) Sort Order Enables specification of the sort order (such as Alphabetical ascending or descending, numerical ascending or descending, Date Time ascending or descending) and the sort field that the parameter values are sorted on.
13) Import and Export We can import or export the defaults values.

 After designing done our crystal report will be look like below image.
Parameter Fields Crystal Report Designing.png

 Now we degin here Page for Call Parameter crystal report. We have pass parameter values to Crystal Report.
  Code: FrmParameterReportDemo.aspx  
<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="FrmParameterReportDemo.aspx.cs" 
Inherits="ProjCrystalReport.FrmParameterReportDemo" %>

<!DOCTYPE html
 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/x
html1-transitional.dtd">

<html xmlns=
"http://www.w3.org/1999/xhtml">
<head runat="
server">
    <title></title>
</head>
<body>
    <form id="
form1" runat="server">
    <div>
    <h1>Crystal Report's Parameter Fields Example</h1>
        <asp:Button ID="btnDownload"
runat="server" 
Text="DownLoad Pdf"
 onclick="btnDownload_Click"  />
    </div>
    </form>
</body>
</html>

  Code: FrmParameterReportDemo.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using System.Configuration;
using System.Data.OracleClient;

namespace ProjCrystalReport
{
    public partial class FrmParameterReportDemo :
System.Web.UI.Page
    {
        protected void btnDownload_Click(object sender, EventArgs e)
        {           
            GenerateEmpReport();
        }
        public void GenerateEmpReport()
        {
            String ReportPath = "";
            String ExportPath = "";
            String PDFNAME = "ParameterFieldsExample" + 
DateTime.Now.ToString
("ddMMyyyyhhmmss") + ".pdf";
            String creatfpdf = Server.MapPath(@"~/GarbageBin/");
            var finalPDF = System.IO.Path.Combine(creatfpdf, PDFNAME);

            ReportPath = Server.MapPath("~\\CrtParameterFieldsExample.rpt");
            ExportPath = Server.MapPath("~\\GarbageBin\\") + PDFNAME;

            String[] Parameter = new String[2];
            var ParameterVal = new dynamic[2];
            Parameter[0] = "EmpName";
            ParameterVal[0] = "abc";
            Parameter[1] = "EmpMobNo";
            ParameterVal[1] = 9999999999;


            ShowReport(EmpDetRpt, ReportPath, ExportPath, Parameter, ParameterVal);
            System.Net.WebClient client = new System.Net.WebClient();
            Byte[] buffer = client.DownloadData(finalPDF);

            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length",
buffer.Length.ToString());
                Response.BinaryWrite(buffer);
                Response.End();
            }           
        }

        public static void ShowReport(String 
RptPath,String ExportPath, String[] Parameter, 
dynamic ParameterValue)
        {
            ReportDocument rpt = new ReportDocument(); 
            rpt.Load(RptPath);

            for (int i = 0; i < Parameter.Length; i++)
            {
                rpt.SetParameterValue(Parameter[i], ParameterValue[i]);
            }

            rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.
PortableDocFormat, ExportPath);
            rpt.Close();
            rpt.Dispose();
        }
    }
}

Output
Crystal Report's Parameter Fields.gif

  That's it we have created a Parameter Fields Report using Crystal Reports and displayed it within browser in an Asp.Net page.
logoblog

Thanks for reading Parameter in Crystal Report in Asp.Net

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.