Pass Crystal Report Parameters Programmatically Asp.Net

Pass Crystal Report Parameters Programmatically In Asp.Net 2.0,3.5,4.0 Using C# And VB.NET. In this post i am explaining how to pass parameters to crystal reports programmatically in code behind of web page.

Pass Crystal Report Parameters Programmatically In Asp.Net

For this example i am using northwind database and products table.

I have put one text box on the page and report will display details of product based on product id entered by user.

Read how to create Crystal Reports In Asp.Net , Or Windows Forms.

Open crystal report in design view, right click on it and select Field Explorer

Now select Parameter Fields and select new to add new parameter to report.

Name it as ProductID and remember it.

Now click on Special Fields in Field Explorer and select Record Selection Formula.

Select is equal to and {?ProductID} from the dropdowns and click on OK.

Click on smart tag of reportviewer control and uncheck Database logon prompting and parameter prompting as we will provide these info in code behind.

HTML markup of aspx page

<form id="form1" runat="server">
    <table class="style1">
                Enter Product ID :
                <asp:TextBox ID="txtProductID" runat="server">
                <asp:Button ID="btnReport" runat="server" 
                            Text="Show Report" 
                            Width="108px" />
    <br />
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
        AutoDataBind="True" EnableDatabaseLogonPrompt="False" 
        EnableParameterPrompt="False" Height="1039px" 
        Width="901px" DisplayGroupTree="False" />
    <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
        <Report FileName="CrystalReport.rpt">

Now go to code behind of the page and add below mentioned namespace for crystal reports.

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

Write this code in Page_Load event of the page

protected void Page_Load(object sender, EventArgs e)
        if (Page.IsPostBack) CrystalReportViewer1.Visible = true;
            CrystalReportViewer1.Visible = false;

Generate click event for button to shaow report and write this code.

protected void btnReport_Click(object sender, EventArgs e)
    {   //Create report document
        ReportDocument crystalReport = new ReportDocument();
        //Load crystal report made in design view

        //Set DataBase Login Info
            ("amitjain", "password", @"AMITJAIN\SQL", "Northwind");

        //Provide parameter values
        crystalReport.SetParameterValue("ProductID", txtProductID.Text);
        CrystalReportViewer1.ReportSource = crystalReport;

Build the solution and run.

have fun.

If you like this post than join us or share


Anonymous said...

great job done .........thank alot

Anonymous said...

beautifuly explained, thanks a lot

Anonymous said...

My scenario is similar to your article topic

Pass session value in sql query

table 1 structure

scheme_code, scheme_name

101 Fund1
102 Fund2


ctr, user_id,scheme_id,amt_invested
1 admin 101 7000
2 admin 101 10000
3 usr1 102 6000
4 usr2 101 30000

When the logged in user is admin,

I want records 1 &2 records to be displayed like this

scheme_name,amt_invested (these are the fields to be displayed in the report)

fund1 7000
fund2 10000

if usr1 is logged, then record 3

Fund2 6000

should be displayed and so on

for this i am using the query,

SELECT table1.scheme_name,table2.amt_invested from table2 inner JOIN table1 ON table2.scheme_id=table1.scheme_code and table2.user_id='" + Session["user_id"] + "';

I included the following lines

ParameterFields paramFields = new ParameterFields();

ParameterField pfItemYr = new ParameterField();

pfItemYr.ParameterFieldName = "user_id"; //Employee ID is Crystal Report Parameter name.

ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();

dcItemYr.Value = Session["user_id"].ToString();



CrystalReportViewer1.ParameterFieldInfo = paramFields;

but still the records are repeated.

pl help

Saif said...

You are cool man....Thanks a ton

Ravi said...

It helps alot thnx..!! :)

Find More Articles