Crystal Reports for Visual Studio 2005 in CSharp - Exercise 4: Bind your Crystal Report to the Crystal Report Viewer (Page 3 of 5 ) Scenario In this exercise, you will bind the Stock Objects report to the Crystal Report Viewer, set the data source of the report to an Object Collection, and populate the Object Collection programmatically. Tasks Detailed Steps - Binding the Report to the Crystal Report Viewer.
- Switch to the default Code-Behind class, Default.aspx.cs.
- Above the class signature, add a "using" declaration to the top of the class for the System.Collections namespace.
using System.Collections;
This reference gives you access to the ArrayList class. ArrayList implements ICollection. This qualifies ArrayList as one of several class types that can be used to build an object collection that is recognized by Crystal Reports.
- Add a new class-level ArrayList, call it stockValues.
private ArrayList stockValues;
- Add a new class-level declaration for the ReportDocument report wrapper class, with the variable name stockObjectsReport. Set its access modifier to private.
private ReportDocument StockObjectsReport;
- Within the ConfigureCrystalReports()method you created in Exercise 1: Web Application Setup, declare a string variable, name it reportPath, and assign to it a runtime path to the local report. Pass the name of the local report file as a string parameter into the Server.MapPath() method. This maps the local report to the file path at runtime.
String reportPath = Server.MapPath("StockObjects.rpt");
- Add two line breaks, and instantiate the ReportDocument class.
StockObjectsReport = new ReportDocument();
- On the next line, Call the Load()method of the ReportDocument instance and pass into it the reportPath string variable.
StockObjectsReport.Load(reportPath);
The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine namespace. You have added a "using" [C#] declaration for this namespace in Exercise 1: Web Application Setup. When you instantiate ReportDocument and load a report, you gain access to the report through the SDK.
- Next, set the data source of the report to the stockValues ArrayList.
StockObjectsReport.SetDataSource(stockValues);
- Finally, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.
crystalReportViewer.ReportSource = stockObjectsReport;
At this point, the Stock Objects report is bound to the Crystal Report Viewer and the page displays the correct report; however, the report is currently bound to an empty data source, thus the report has no information to display. In the next step, you will programmatically populate the stockValues ArrayList with sample data.
- Populating the Object Collection Programmatically.
In this task, you will add Session code to the ASPX code-behind class. If there are no values currently held in session, default values will be created. If there are values in session, they will be assigned to the stockValues ArrayList.
- Within the class, add a new public scope helper method, with no return value, named PopulateStockValuesArrayList().
public void PopulateStockValuesArrayList() { }
- Within the PopulateStockValuesArrayList()method, before the existing code, create an if/else conditional block that checks whether a Session object named stockValues exists.
if(Session["stockValues"] == null) { } else { }
- Within the If block, instantiate a new ArrayList().
stockValues = new ArrayList();
- Next, use the overloaded constructor for the Stock class to create and instantiate three instances of Stock.
Stock s1 = new Stock("AWRK",1200,28.47); Stock s2 = new Stock("CTSO",800,128.69); Stock s3 = new Stock("LTWR",1800,12.95);
- Add these three instances to stockValues.
stockValues.Add(s1); stockValues.Add(s2); stockValues.Add(s3);
- Add the updated stockValues ArrayList into session.
Session["stockValues"]=stockValues;
- Inside of the Else block, write a line to assign the current values held in session to the stockValues ArrayList.
stockValues = (ArrayList)Session["stockValues"];
- Finally, call the PopulateStockValuesArrayList() from the ConfigureCrystalReports()method.
- This should be the first line of code executed in the ConfigureCrystalReports() method.
PopulateStockValuesArrayList();
- From the Build menu, click Build Solution.
If you have any build errors, fix them now.
- From the Debug menu, click Start Debugging.
- If this is the first time you have started debugging, a dialog box appears and states that the Web.config file must be modified. Click the OK button to enable debugging.
The Default.aspx page loads into your browser with three default values.
- Close the Internet Explorer window.
 | Take Microsoft software for a test drive. With MSDN Virtual Labs, you get full access to all available Microsoft products through 90-minute modules, each with its own downloadable manual. Try this lab out now. |
Next: Exercise 5: Adding Data Dynamically to the Stock Report >>
More C# Articles More By MSDN Virtual Labs |