Building Reports Using ASP.NET and Crystal Reports - Part 2 - Quarterly Sales Report
page 13 of 15
by Vince Varallo
Feedback
Average Rating: 
Views (Total / Last 10 Days): 63291/ 123

Step 12: Viewing the report in an ASP.NET page

Now that you have built the report you can build a web page to display it. I will create a simple page that lets the user enter a fiscal year and view the report.

1.    Right click on the Adventure Works web site in the Solution Explorer. 

2.    Select Add New Item… from the pop-up menu.

3.    Select Web Form from the templates and change the name to SalesReport.aspx. Make sure the Language is set to C# and click the Add button.

4.    Open the page and view its markup. Add the following Register directive after the Page directive.

Listing 4

<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, 
PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR"%>

This allows you to use the Crystal Reports Viewer control that comes with Visual Studio.

5.    Add the following code between the div tags.

Listing 5

<asp:TextBox runat="server" ID="txtFiscalYear"></asp:TextBox>
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click" 
Text="Preview" />
<br />
<br />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
AutoDataBind="true" />    

This adds a text box that will allow the user to enter the fiscal year to print the report.

6.    Add the following using statements in the code behind.

Listing 6

//Custom using statements
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

7.    Add the following code the Page_Load event.

Listing 7

if (IsPostBack)        
{
    if (CrystalReportViewer1.Visible == true)
    {
        //Rebind the report.
        BindReport();
    }
}

8.    Add the following code for the Preview button's click event. You can create the event handler by double clicking on the button in Design mode.

Listing 8

protected void btnPreview_Click(object sender, EventArgs e)
{
    BindReport();
    CrystalReportViewer1.Visible = true;
}

This code calls a custom method called BindReport() and then shows the Crystal Report Viewer Control.

9.    Now add the following custom methods.

Listing 9

private void BindReport()
{
    ReportDocument report = new ReportDocument();
    report.Load(Server.MapPath("Sales.rpt"));
 
    SetTableLocation(report.Database.Tables);
 
    report.DataDefinition.FormulaFields["FiscalYear"].Text = txtFiscalYear.Text;
 
    CrystalReportViewer1.ReportSource = report;
}
 
private void SetTableLocation(Tables tables)
{
    ConnectionInfo connectionInfo = new ConnectionInfo();
 
    connectionInfo.ServerName = @"LTMTI30\SQL2008";
    connectionInfo.DatabaseName = "AdventureWorks";
    connectionInfo.UserID = "aspalliance";
    connectionInfo.Password = "aspalliance";
 
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
        TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
        tableLogOnInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(tableLogOnInfo);
    }
}

The first method creates an instance of the ReportDocument class. This represents the report that you created earlier and allows you to manipulate it at runtime. The SetTableLocation() method sets the table location for each table in the report. Again, this assumes you have created an "aspalliance" SQL Login and have given it access to the database. The FiscalYear formula is then set to the year the user typed into the report. You could add validation to make sure the user entered a four digit year, but for simplicity I left this out. The Crystal Report Viewer's source is then set to the report object. 

You can now run the project. Set the SalesReport.aspx page to the start page and run the project. Enter 2002 for the Fiscal Year and click the Preview button.

Figure 13

You can scroll through the report using the navigation buttons at the top of the page or you can click directly to a sales territory by clicking the name in the group tree on the left hand side of the report.


View Entire Article

User Comments

Title: specialized in wholesale cheap nike nfl jerseys   
Name: wholesale cheap jerseys
Date: 2012-08-06 4:16:47 AM
Comment:
we are specialized in wholesale cheap nike nfl jerseys , nfl jerseys , mlb jerseys , nhl jerseys , nba jerseys , soccer jerseys ,hoodies, sports caps.

http://jerseymember.com/Nike-NFL-Jersey-c134.html

http://jerseymember.com/NFL-Jersey-c68.html

http://jerseymember.com/MLB-Jerseys-c94.html

http://jerseymember.com/NHL-Jerseys-c96.html

http://jerseymember.com/NBA-Jerseys-c77.html

http://jerseymember.com/NCAA-Jersey-c114.html

http://jerseymember.com/Soccer-Jerseys-c123.html

http://jerseymember.com/Customized-Jerseys-c116.html

http://jerseymember.com/Customized-Jerseys-c116.html

http://jerseymember.com/Hoodies-c136.html

http://jerseymember.com/Sports-Caps--c119.html

http://jerseymember.com/Others-c131.html
Title: nfl jerseys cheap   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:11:39 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:10:03 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: asp   
Name: sumti
Date: 2011-04-10 7:27:41 AM
Comment:
very nice code
Title: Nice Article   
Name: Rick
Date: 2009-12-01 11:18:40 AM
Comment:
The example was very easy to understand and follow, thanks for taking the time to put these together.
Title: Awesome   
Name: Rusty
Date: 2009-08-27 10:36:14 AM
Comment:
Awesome job. I am a brand new CR user and I found your article to be easy to understand, follow, and very helpful. Thanks and keep them coming.
Title: Superb   
Name: ramakrishna
Date: 2009-08-01 6:51:52 AM
Comment:
This article is very good to customise my website,
Title: great work   
Name: Ankit Agarwal
Date: 2009-07-28 12:48:39 AM
Comment:
u have done a great work
this article is really helpful
Title: Great Job   
Name: yin min
Date: 2009-07-26 5:52:54 AM
Comment:
Thanks for your step by step article.
Everyone who is new at crystal are looking for such a simple and very useful article.
Thanks again.
Title: Well Done   
Name: Muhammad Aijaz Rajput
Date: 2009-07-10 10:47:53 AM
Comment:
This is an excellent article. Thanks. I realy need the detailed step-by-step article approach that you take.
Title: Source coming soon   
Name: Vince
Date: 2009-06-22 9:20:19 AM
Comment:
The source should be posted soon.
Title: Excellent   
Name: Walter
Date: 2009-06-16 4:03:31 AM
Comment:
Hi Vince,

another excellent article. Thanks. I really like the detailed step-by-step approach that you take.

Will the source be available as a download?

Regards

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-18 5:52:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search