How to pass set Crystal Report Parameter value from C# VB.Net in runtime

When you design a Crystal Report most of the time you need to use Crystal Report parameter specially to design report header. Like you are going to output a report which contains a certain date data or may be a date range. So that you need to write the date range or date into the report header. In this scenario you need to use Crystal Report parameter because the data (body) you will bind, may or may not contain the date or date range. User select a date or date range from front end & you will filter those data to bind the report as well as you need to set the corresponding date or date range value to report header by sending the date or date range to crystal report as parameter(s). Here i will show you how you can set or pass one or more or multiple crystal report parameter from C# & VB.Net aspx page code behind in run time.

To know how to bind data please read the below URL:
Dynamically Runtime Bind Data into Crystal Report using C# VB.Net
Note: To run the below example please collect SQL Server Table schema & sample data from above URL.

Please follow the steps below:
1. Add a crystal report in your project
2. From Field Explorer right click on Prameter Fields node & click on New:
Pass Crystal Report parameter

3. Define the parameter name & Datatype:

4. Expand Parameter Fields. Drag & Drop the “date” parameter into the Report Header in Design View:

5. Now Add an aspx page into your project & set the datasource & parameter value in the following way:
C# Code:

        if (!IsPostBack)
            DataTable dt;
            String SQL = "SELECT Roll,Name,Email,Address,AdDate FROM Student";

            string sConstr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(sConstr))
                using (SqlCommand comm = new SqlCommand(SQL, conn))
                    using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        dt = new DataTable("tbl");

            ReportDocument _rdStudents = new ReportDocument();
            string reportPath = Server.MapPath("Students_CrystalReport.rpt");


            _rdStudents.SetParameterValue("date", DateTime.Now); // Add multiple parameter as you want
            CrystalReportViewer1.ReportSource = _rdStudents;

Note: Don’t forget to add below Namespaces:

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

VB.Net Code:

        If Not IsPostBack Then
            Dim dt As DataTable
            Dim SQL As String = "SELECT Roll,Name,Email,Address,AdDate FROM Student"

            Dim sConstr As String = ConfigurationManager.ConnectionStrings("TestConnection").ConnectionString

            Using conn As New SqlConnection(sConstr)
                Using comm As New SqlCommand(SQL, conn)
                    Using da As New SqlDataAdapter(comm)
                        dt = New DataTable("tbl")
                    End Using
                End Using
            End Using

            Dim _rdStudents As ReportDocument = New ReportDocument()
            Dim reportPath As String = Server.MapPath("Students_CrystalReport.rpt")


            _rdStudents.SetParameterValue("date", DateTime.Now) 'Add multiple parameter as you want
            CrystalReportViewer1.ReportSource = _rdStudents

        End If

Note: Don’t forget to add below Namespaces:

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Please note that “set the parameter value into the report after data binding”. Otherwise you will get a prompt to enter data.

Now run the project & check that the report shows the date in Report Header that you send from aspx page in runtime:


Yet confusion. Download below code example. Don’t forget to modify Connectionstring first.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code