Building Reports Using ASP.NET and Crystal Reports - Part 6 - Build a Sales Forecast Report Using Parameter Fields
page 10 of 12
by Vince Varallo
Feedback
Average Rating: 
Views (Total / Last 10 Days): 63149/ 78

Step 9: 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'll create a simple page that lets the user click a button to preview the report.  We'll do all of this in this Default.aspx page that is part of the web site we created.

1.    Open the Default.aspx page and view its markup.  Add the following Register directive after the Page directive.

<%@ 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.

2.    Add the following code between the div tags.  

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

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

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

4.    Add the following code for the Preview button's click event.

protected void btnPreview_Click(object sender, EventArgs e)
{
    BindReport();
 
    ViewState["ParametersShown"= "True";
}

This code calls a custom method called BindReport() and then stores a value of true to persist the fact that the parameters have been shown to the user.  Since we are using the Crystal Viewer Control it will automatically display a page to collect the values for the parameters in the report.

5.    Now add the following custom methods.

private void BindReport()
{
    ReportDocument report = new ReportDocument();
    report.Load(Server.MapPath("SalesForecast.rpt"));
 
    SetTableLocation(report.Database.Tables);
 
    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've created an "aspalliance" SQL Login and have given it access to the database.  The Crystal Report Viewer's source is then set to the report object. 

6.    Add the following to the Page_Load event.

//Check if the parmeters have been shown.
if ((ViewState["ParametersShown"] != null) && 
    (ViewState["ParametersShown"].ToString() == "True"))
{
    BindReport();            
}

This will call the BindReport method again once the user has entered the parameters.  Crystal keeps these values on postback.

You can now run the project by pressing F5.  Click the Preview button. You should be prompted for the Sales Forecast Factor, the Start Date, and the End Date.

Enter 5 for the Sales Forecast Factor.  This will increase the sales numbers by 5%.  Click on the icon next to each date to set the start date to 1/1/2002 and the end date to 12/31/2002.  Now click the Submit button.

The first page of the report should show the original sales numbers and the second page will show the forecast sales numbers.


View Entire Article

User Comments

Title: Help   
Name: Arjun
Date: 2010-05-21 4:28:35 AM
Comment:
Hi! anybody body can assit me to generate Accouting report trial balance, profit & loss, and balance sheet. using crystal report with ASP.NET with C#
Title: Nice Article   
Name: Rick
Date: 2009-12-01 4:56:29 PM
Comment:
I read the entire series and I fell more comforable with reports. They were all put together well and every piece of code worked as expected. Thank for taking the time to share.

Product Spotlight
Product Spotlight 



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


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-04 4:59:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search