Filtering Crystal Report's Data Based on Textbox Value Without Logon Prompt

Introduction

This article demonstrates how to filter Crystal Report's data based on textbox value without logon prompt using ASP.NET with C#. Here I have created two web pages and a Crystal Report. One web page for a textbox value and a button and another web page for displaying Crystal Reports.

Problem with single web page

By using a single webpage, the Crystal Report's options export, print, zoom, etc. will not work properly, so this article can shows how to do that with two web pages for proper functionality without a logon prompt.

Database for this sample

I have used the database name "EmpDetails", in that the table will be an "empno" and other columns as follows:

FilCrstlRpt1.jpg

Creating first web page

Create the first web page for the Textbox value for filtering the Crystal Report data. In this add a textbox and a button. See the following designer code:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>.: Filter The Data Here :.</title>

</head>

<body>

    <form id="form1" runat="server">

    <div style="text-align:center; padding:50px 0px 50px 0px;">

        <h1>Filtering Crystal Report Data based on TextBox Value without Logon Promt</h1>

        Enter Designation : <asp:TextBox ID="TxtDesig" runat="server"></asp:TextBox><br />

        <asp:Button ID="BtnReport" runat="server" Text="Show Report" onclick="BtnReport_Click" />

    </div>

    </form>

</body>

</html>

Now write the code in the button click event to take a textbox value in the session and redirect to another page. The code will be as shown below:

protected void BtnReport_Click(object sender, EventArgs e)
{
    Session["JOB"] = TxtDesig.Text;
    Response.Redirect("ReportPage.aspx");
}

Then create the Crystal Reports report.

Creating Crystal Report

First create the Crystal Reports report with Create New Connection -> OLE DB(ADO) -> Select SQL Server provider -> Enter Server Name, User Name,

Password, Database Name -> add table (I used Emp table) -> add columns (I used EmpNo, Name, Job, Sal, Deptno) -> Finish.

Now Field Explorer -> Parameter Fields -> Right-click -> New -> Enter details like below -> Click OK.

FilCrstlRpt2.jpg

Now select Crystal Reports -> Crystal Reports Menu -> Report -> Select Expert… -> select emp.job (Which was filtering column) like below:

FilCrstlRpt3.jpg

Click OK -> in first drop down list select "is equal to" then select Parameter field {?JOB} then click OK as shown below:

FilCrstlRpt4.jpg

Then create the second web page.

Creating second web page

Create the second web page and add a CrystalReportViewer and Source as shown in the following code:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>.: See The Report Here :.</title>

</head>

<body>

    <form id="form1" runat="server">

    <div style="text-align:center; padding:50px 0px 50px 0px;">

        <h1>See the Report Which was Filtered By the value of TextBox</h1><br />

        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"

            AutoDataBind="True" DisplayGroupTree="False" EnableDatabaseLogonPrompt="False"

            EnableParameterPrompt="False" Height="1039px"

            ReportSourceID="CrystalReportSource1" ReuseParameterValuesOnRefresh="True"

            Width="773px" />

        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">

            <Report FileName="EmpReport.rpt">

            </Report>

        </CR:CrystalReportSource>

    </div>

    </form>

</body>

</html>

Then write this code in page load event like in the following.

Namespaces

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

Code

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument rd = new ReportDocument();
    rd.Load("Full Path of Your Report\EmpReport.rpt");
    rd.SetDatabaseLogon("UserName", "Password", "ServerName", "Database");
    rd.SetParameterValue("JOB", Session["JOB"]);
    CrystalReportViewer1.ReportSource = rd;
} 


Finally Build and run the first page; provide input then click the button then it shows the report and all the options like export, print, etc. will work normally.
Enjoy Programming.

Summary

In this article, I discussed how we can filter the Crystal Report data based on a textbox value without logon prompt. As I discussed previously the problem with Crystal Reports controls will be solved through this method of creating two web pages. And also I have used a simple session element creation and pass this value from one page to another page.
 


Similar Articles