Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Crystal Reports allow you to display the data in a formatted report style form. You can easily create Crystal Reports by adding the "New Item" from the Visual Studio and going through the wizard. The problem comes when you need to make a report that is dependent on the parameters being passed from the ASP.NET application. In this blog entry I will quickly go over the method in which you can pass the parameters from ASP.NET application to Crystal Reports and display them in a webform. I will soon write a complete article about this scenario.

Please also note that if all the steps are performed correctly the total time to produce a report dependent on the parameters is only 2 MINUTES

I am using the Northwind database. The category names from the "Categories" table are displayed in the DropDownList. Once, the category is selected the corresponding products are displayed as a report on the webform. The first thing that you must do is to add a Crystal Report in the project. Once, the report is added you can select the tables. Since, we are dealing with Categories and Products you need to select both the tables. When you select tables it will show you the join between them which will be on CategoryID. Now, you must add the ParameterField to the report. The ParameterField have to be "CategoryID" since your result is based on the selection of Category. You can simply open the report and select ( View -> Other Windows -> Document OutLine ) this will display the additional features you can add with the report. Then select the PropertyField and type the name of the field as "CategoryID" and make it a "Number" datatype.

After this you need to configure the "Select Expert". Simply, select "Crytal Reports" from the top menu then select "Report" -> "Select Report". Here you can adjust the parameter to "is equal to" and then select the "CategoryID" from the parameter list.

At this point you are set from the Crystal Reports side now you need to do some configuration on the ASP.NET side. First you need to fill the DropDownList with the category name and categoryID as DataTextField and DataValueField respectively.

After this you need to implement the button click event which will take the categoryID from the DropDownList and publish the report based on the categoryID.

Here is the complete code for the page:

Code behind:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

public partial class _Default : System.Web.UI.Page

{

private ReportDocument report = new ReportDocument();

 

protected void Page_Load(object sender, EventArgs e)

{

report.Load(Server.MapPath("CrystalReport.rpt"));

report.FileName = Server.MapPath("CrystalReport.rpt");

 

if (!Page.IsPostBack)

{

BindData();

}

}

protected override void OnUnload(EventArgs e)

{

base.OnUnload(e);

this.Unload+=new EventHandler(Page_Unload);

}

public void Page_Unload(object sender, EventArgs e)

{

// clean up the resources

report.Clone();

report.Dispose();

}

private void BindData()

{

string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryName,CategoryID FROM Categories", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

ddlCategory.DataSource = ds;

ddlCategory.DataTextField = "CategoryName";

ddlCategory.DataValueField = "CategoryID";

ddlCategory.DataBind();

}

// This will display the report

protected void Btn_DisplayReport(object sender, EventArgs e)

{

// get the selected CategoryID

int categoryID = Convert.ToInt32(ddlCategory.SelectedValue);

report.SetParameterValue("CategoryID", categoryID);

CrystalReportViewer1.ReportSource = report;

}

}

 

And here is the HTML part of the code:

<div>

Select a category: <asp:DropDownList ID="ddlCategory" runat="server" >

</asp:DropDownList>

<asp:Button ID="Btn_Display" runat="server" Text="Display Report" OnClick="Btn_DisplayReport" />

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

</div>

Posted on Wednesday, September 13, 2006 9:17 PM | Back to top


Comments on this post: Passing Parameters from ASP.NET Application to Crystal Report Application

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Hi, I want ur help in passing paramaeters to crystal reports in asp.net 2003. my code behind is vb.net
so can u send me some tutorial how to pass paramters.

my email address is: shah.dhruv@rediffmail.com
Left by Dhruv Shah on Nov 24, 2006 11:59 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Works good for me dyamically displaying reports, but the print nor export seem to work.
Left by IronSights on Dec 15, 2006 8:50 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Hi,

The button click event part doesn't works Give me any other suggestions for this..

Thank u
Left by Santhana Krishnan on Dec 22, 2006 2:14 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Good One
Left by Rahul Kale on Feb 21, 2007 10:08 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
as ur code..
if u specify page_unload it will take lot of time to load application.
Left by Subramanian on Apr 25, 2007 8:11 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
page didnt loaded if specify page_unload event ..what will do?
Left by Subramanian on Apr 25, 2007 8:12 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
With Studio 2005 and Crystal XI its pretty straightforward in vb now:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Create ReportDocument which is code replacement for default.aspx reportsourceDim icnetreport As ReportDocumenticnetreport = New ReportDocument'get report path, load in report documentDim reportPath As String = Server.MapPath("CrystalReport1.rpt")icnetreport.Load(reportPath)'Preload parameters ... cannot mix set parameter and user input.icnetreport.SetParameterValue("Ship via", "FedEx")icnetreport.SetParameterValue("begdate", "2000-2-1")icnetreport.SetParameterValue("enddate", "2001-8-1")CrystalReportViewer1.ReportSource = icnetreportEnd Sub

There is more information in the instant guide here:
www.instantcrystal.net
Left by jeni on Jun 06, 2007 7:37 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
hi,

i have just one query

while createing report i am binding with data set and as written in artical i am passing id to report

and my data set tableaddeptor query is select * from caregories where categoryid = @ id is this correct or i did wrong...... because i am getting error
Left by riddhi on Sep 20, 2007 12:24 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Hi,
I gone through procedure given as above..
But still im Unable to bind it with Data Set..
How to overcome this??????
Im getting error..
Left by Richa on Dec 18, 2007 9:29 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
nice. very nice
Left by Ji on Feb 14, 2008 5:21 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
thanX for given example

Left by ashutosh prashar on Apr 01, 2008 6:56 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
i am looking for this tutorial all the times, thx
Left by willionsha on May 01, 2008 2:40 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
very helpful
Left by anju on May 15, 2008 6:09 PM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
if i bind report using dataset, then it shows a form to fill parameter values, even if i set the values in .cs
i dont want form to be displayed..
please help...

code:
CrystalReport1.Class1Crstal.insert_Selected_Doctor_labels_Reports_Temp(uid, session_id);
OdbcDataAdapter adp = new OdbcDataAdapter();
adp = Class1Crstal.get_CrystalReports_Doctor_labels_Reports(uid, session_id);
DS = new DataSet();
adp.Fill(DS, "Bill_Header_Report_Temp");
rptDoc.Load(Server.MapPath("~/Reports/51_dr list.rpt"));
rptDoc.FileName = Server.MapPath("~/Reports/51_dr list.rpt");


CrystalReportViewer1.Visible = true;

//CrystalReportViewer1.ReportSource = rptDoc;
string categoryID = "SP1";
rptDoc.SetParameterValue("CategoryID", categoryID);
string Line1 = "SP2";
rptDoc.SetParameterValue("Line1", categoryID);
string Line2 = "SPE2";
rptDoc.SetParameterValue("Line2", categoryID);
string Line3 = "SPEC2";
rptDoc.SetParameterValue("Line3", categoryID);
rptDoc.SetDataSource(DS);
CrystalReportViewer1.ReportSource = rptDoc;

Left by prad on Dec 08, 2010 12:31 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Very Good, Short & Sweet
Left by Sunil Mahla on Dec 12, 2010 6:33 AM

# re: Passing Parameters from ASP.NET Application to Crystal Report Application
Requesting Gravatar...
Very Useful Example
Left by Nayana on Dec 31, 2010 1:24 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net