Vs2005+crystal, Web application development

Source: Internet
Author: User
Tags bind file system header insert modify visual studio
Web

   SummaryThis experiment will help you create a Web application using the Crystal Report-the data from the reports comes from a collection of objects, and you will create a special class to hold the stock market data.

   I. Development objectives

After completing this experiment, you will be able to better:

· Create a special class to hold the stock market value.

· Instantiate this class.

· Populates an object collection with data.

· Dynamically add data through a Web form.

· Use Crystal Report Designer to skillfully create a crystal statement.

   ii. Description of the content

This experiment will lead you to create a Crystal Reports Web application-The data in the report comes from a collection of objects. The application was successfully developed with Crystal Reports for Visual Studio 2.

You will then create a specialized class to hold the stock market values, instantiate the class and populate an object collection with data, and add data dynamically through a Web form. You will then create a crystal report by using the Crystal Reports designer control-it connects to the object collection and dynamically generates a chart and stock summary information.

   Third, exercise one: Web application installation

Note In this exercise, you will create a new Web site and apply the standard settings to complete this exercise.

Detailed steps

1. Create a new ASP.net Web site in Visual Studio.

· Double-click the Visual Studio 2 shortcut on your desktop.

· Click "File | new | website".

· In the new Web Site dialog, click ASP.net web site.

· In the location list, select File System.

· In the Language list, select Visual C #.

· In the Location text field, accept the default path and name.

· Click OK.

2. Add Crystal the Viewer control.

· From Solution Explorer, double-click Default.aspx to open the Web form.

· Click the Design button at the bottom of the form to change the Web form to Design view.

· From the toolbar, expand the Crystal Reports node and navigate to the Crystalreportviewer control.

· Drag the Crystalreportviewer control to the Web form.
 
· If the Crystalreportviewer Tasks on the Smart Task panel is turned on, press ESC to close it.

· The Smart Task panel is a new feature in Visual Studio 2005-allowing less code to create a project. In this tab, you will use a programmatic approach, so you do not have to use the Smart Task panel.

· Click the Properties tab and select Crystalreportviewer.

· From the Properties window, set the ID property to Crystalreportviewer.

· From the File menu, click SaveAll.

3. Add the programming environment.

· Click the Solution Explorer tab.

· In Solution Explorer, right-click Default.aspx and click "View Code".

· On top of the class signature, use the "using" statement to add a reference declaration to the assembly-set namespace.

Using CrystalDecisions.CrystalReports.Engine;
Using Crystaldecisions.shared;
· In this class, add a new private type with no return value for the Assistant Method Configurecrystalreports ().

private void Configurecrystalreports ()
{}
· Configurecrystalreports () is an assistant method-it is responsible for interacting with the report at run time. It is also responsible for programmatically interacting with reports. In order to properly configure Crystalreportviewer, it must be called from Page_Init ().

· Add the Page_Init event handler using the appropriate syntax shown.

private void Page_Init (object sender, EventArgs e)
{}
· In a C # Web Form in Visual Studio 2005, the Page_Init event handler in the Code-behind class is automatically attached to the Init event. This event handler signature must match correctly for invocation.

· Finally, in the Page_Init event handler, add a call to the Configurecrystalreports () Assistant method.

Configurecrystalreports ();
· From the File menu, click Save All.

· Now, you are ready to create your custom stock market information class.

   Iv. Exercise 2: Create a customized stock market information

Note In this exercise, you create a custom class to hold stock market information. This class will be used to populate an object collection.
Detailed steps

· In Solution Explorer, right-click the bold Web site name and click "Add New Item".

· Then, the ADD New Item dialog box appears.

· In the visual Studio installed Templates field, select Class.

· In the Name field, enter the stock, and then click Add.

· Click Yes in the dialog box that appears later.

· In Visual Studio 2005, all classes must be placed under an app code folder, if they are to be used. When you click the Add button, a warning dialog box appears asking if you want to put your class under this App_Code folder.

· The stock class in this article must be set to the public class so that you can access it when you create a report. Make sure you create a class that is public.

public class Stock
{
Public stock ()
{}
}
· In this class, add three private fields.

private string _symbol;
Private double _price;
private int _volume;
· You will then add three public read/write properties to encapsulate the three private fields.

· Creates a new property, named symbol.

public string Symbol
{
Get
{
return _symbol;
}
Set
{
_symbol = value;
}
}
· Creates a new property, named Price.

Public double Price
{
Get
{
return _price;
}
Set
{
_price = value;
}
}
· Creates a new property, named volume.

public int Volume
{
Get
{
return _volume;
}
Set
{
_volume = value;
}
}
· Finally, a new constructor is created that uses three public properties as parameters.

Public stock (String symbol, int volume, double price)
{
_symbol = symbol;
_volume = volume;
_price = Price;
}
· From the Build menu, click Build Website.

· If you have any build errors, modify them now.

· Now, you are ready to access this object from the built-in crystal-designer.

   v. Exercise 3: Create a Crystal Report

Note In this exercise you will create a new crystal report in the built-in Crystal Reports designer, and then bind this to the stock object.

Detailed steps

· Right-click the site name and click "Add New Item".

· In the ADD New Item dialog box, select Crystal.

· In the Name field, enter Stockobjects.rpt, and then click Add.

· In the Crystal Reports Gallery dialog box, click OK.

· In the Standard Creation Wizard dialog box, expand Project Data and child node. NET Objects.

· A list of classes in the project will appear.

· Expand the stock class to see a selectable child node.

· Click the arrow to the right to move the "stock" subclass node to the Selected Tables panel.

· Click Next.

· Expand the stock and click ">>" to move all columns to the "Fields to Display" panel.

· Click Next.

· Select symbol and click the arrow to the right to move it to the Group by panel.

· Click Finish.

   Vi. Exercise 4: Bind your Crystal Reports to the Crystal Report Viewer

In this exercise, you will bind the Stock object report to the Crystal Reporting Viewer, set the report's data source as a collection of objects, and populate an object collection programmatically.

Detailed steps

1. Bind the reports to the Crystal Report Viewer.

· Switch to the default Code-behind class, Default.aspx.cs.

· Above the class signature, add a reference to the namespace System.Collections.

Using System.Collections;

· This reference allows you to access the ArrayList class, and ArrayList implements the ICollection. This makes ArrayList one of several types of classes used to build a collection of objects that are recognized by Crystal Reports.

· Add a new class-level ArrayList named Stockvalues.

Private ArrayList stockvalues;
· Add a new class-level declaration for the Reportdocument report wrapper class with the variable named Stockobjectsreport. and set it to private.

Private Reportdocument Stockobjectsreport;
· In the Configurecrystalreports () method you created in Exercise 1, declare a string variable, name it Reportpath, and assign a run-time path to the local report to it. Pass the name of the local report file as a string reference to the Server.MapPath () method. In this way, the local report is mapped to the Run-time file path.

String Reportpath = Server.MapPath ("Stockobjects.rpt");
· Instantiate the Reportdocument class.

Stockobjectsreport = new Reportdocument ();
· In the next line, call the load () method of the Reportdocument instance and pass the Reportpath string variable to it.

Stockobjectsreport.load (Reportpath);
· This reportdocument class is crystaldecisions.crystalreports. A member of the engine namespace. You have added a declaration to this namespace in practice 1:web application installation (in C #, using "use"). When you instantiate reportdocument and load a report, you can access the report through the SDK.

· Then, set the data source for this report to "Stockvalues ArrayList".

Stockobjectsreport.setdatasource (stockvalues);
· Finally, bind the Crystalreportviewer reportsource attribute to the Reportdocument instance.

Crystalreportviewer.reportsource = Stockobjectsreport;
L. Now, this Stock object report is bound to the Crystal Report Viewer and the page displays the correct reports; However, the current report is bound to an empty data source, so there is no information to display in the report. In the next step, you will populate the Stockvalues ArrayList with the sample data programmatically.

2. Populate an object collection programmatically.

3. In this task, you will add the session code to the ASPX Code-behind class. If there is no value in the session, the default value is generated. If there are values in the session, they are assigned to the Stockvalues ArrayList.

· In this class, add a new public-scoped assistant method with no return value, named Populatestockvaluesarraylist ().

public void Populatestockvaluesarraylist ()
{}
· In the Populatestockvaluesarraylist () method, create a if/else condition block before the existing code-it checks for the presence of a Session object named Stockvalues.

if (session["stockvalues"] = = null)
{}
Else
{}
· In this if block, instantiate a new ArrayList ().

Stockvalues = new ArrayList ();
· Then, use the overload builder of the stock class to create and instantiate three instances of the 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 the stockvalues.

Stockvalues.add (S1); Stockvalues.add (S2); Stockvalues.add (S3);
· Add the updated Stockvalues ArrayList to the session.

session["Stockvalues"]=stockvalues;
· In the Else block, add a line of code to assign the current value in the session to Stockvalues ArrayList.

Stockvalues = (ArrayList) session["Stockvalues"];
· Finally, call 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, modify them now.

· From the Debug menu, click "Start Debugging".

· If this is the first time you start the debugger, a dialog box will appear telling you that you must modify the Web.config file. Click the OK button to start the debugging function.

· The Default.aspx page is loaded into your browser, along with three default values.

· Close the Internet Explorer window.

   Vii. Exercise 5: Dynamically add data to the stock report

Note In the previous exercise, you populate the collection of objects programmatically. In this exercise, you will learn how to dynamically add information from your Web site to your data source. This information can be updated automatically in your report.

Detailed steps

1. Add controls to the Web form.

· Open the Default.aspx file in the Design view.

· To open an ASPX page in Design view, open the file first, and then click the Design button at the bottom of the form.

· Click the Crystalreportviewer control to select it.

· Press the LEFT ARROW key so that the blinking cursor appears and press ENTER to move down four times from the view.

· From the Toolbox, drag a TextBox control to the Web form.

· From the property menu, set the ID to symbol.

· Drag the second TextBox control to the Web form. Put this second textbox below the first one.

· From the property menu, set the ID to price.

· Drag the third TextBox control to the Web form. Put the third TextBox control below the second.

· From the property menu, set the ID to volume.

· Now you can see that adding text to each text box helps to identify which control is corresponding to which parameter.

· Then, from the Toolbox, drag a button control onto the Web form. Put the button under these three TextBox controls.

· From the property menu, set the button's ID to addstockinformation.

· Set the button's text to "ADD-stock information".

· Finally, double-click the "Add Stock Information" button.

· Double-clicking a button control opens the Code-behind class and automatically generates a Addstockinformation_click () event handler.

2. Add information to the collection.

· In the Addstockinformation_click () event handler, a new stock object is created and instantiated.

The stock temp = new stock ();
· In the Addstockinformation_click () method, create a try/catch block.

Try
{}
Catch
{}
· The information entered into a Web form belongs to the string type. Because two fields in the stock class are numerical, you need to write code to convert string values from Web Forms to numerical values. Try/catch statements help protect your Web application from crashes-during a type conversion error match.

· In a try block, assign the value of the Symbol field in the Web form to the symbol property of the stock object.

Temp. Symbol = symbol. Text;

· In the next line, assign the value of the Price field in the Web form to the price property of the Stock object. Notice that the value in the Web form is converted to a double before assigning the value.

Temp. Price = Convert.todouble (price. Text);
· The value of the volume field in the Web form is then assigned to the volume property of the Stock object. Notice that the value in the Web form is converted to an integer before it is assigned.

Temp. Volume = Convert.ToInt32 (Volume. Text);
· Outside the Try/catch block, add the "stock Object" to the Stockvalues ArrayList.

Stockvalues.add (temp);
· Updates the value of the stockvalues that currently exists in the session.

session["stockvalues"] = stockvalues;
· Finally, call the Configurecrystalreports () method. This will rebind the report to this updated stockvalues-collection of objects.

Configurecrystalreports ();
· From the Build menu, click Build Solution.

· If you encounter any build errors, modify them now.

· From the Debug menu, click "Start Debugging".

· If no build errors occur, the Default.aspx page will be loaded into your browser using three defaults. To add additional values, populate the text box appropriately and click the "ADD Stock Information" button. The report will be updated dynamically.

· When you run your site, the report is loaded into your browser window-using three defaults (which you added programmatically in Exercise 4). There are three TextBox controls and one button control above the report. With these controls, you can dynamically update one of your object collections and see that the results of this update are reflected in your report.

· Close the Internet Explorer window.

   Viii. Exercise 6: Add charts and summary information to the report

Now that you have a fully functional web site, it will display a crystal report based on a collection of objects. The site displays information that is entered programmatically into a collection of objects, as well as information that is dynamically added at run time.

In this exercise, you will add two charts, a calculated field and summary information.

Detailed steps

1. Add a chart to the report.

· From Solution Explorer, open the Stockobjects.rpt.

· From the Crystal Reports menu, select Insert and click Chart.

· In the Chart Expert dialog box, select a pie chart.

· Select the Data tab.

· Select Stock.symbol and click the left-right arrow to move the Stock.symbol field to the on field.

· Select Stock.volume and click the right arrow to move the Stock.volume field to the show Value (s) field.

· Click OK.

· Create a new "Table Header" section and add a Chart object to the section.

· From the Debug menu, click "Start Debugging".

· If there is no build error, the Default.aspx page will be loaded into your browser.

· Close the Internet Explorer window.

2. Add a chart based on a formula field. In this section, you will create a chart that displays aggregate information. First, you'll create a formula to calculate a specific stock value, and then create a pie chart-it shows you the proportional value of all your shareholdings.

· From the Crystal Reports menu, select the list and click on "Formula Workshop".

· In the Formula Workshop dialog box, select Formula Fields.

· Click the New button to create an additional formula.

· In the Formula Name dialog box, enter a value.

· Click "Use Editor".

· Add code to multiply the Price field value with the Volume field value.

{Stock.volume}*{stock.price}
· Click Save and Close.

· From the Crystal Reports menu, select Insert and click Chart.

· Under the Chart Expert dialog box, select a pie chart.

· Click the Data tab.

· Select Stock.symbol and click the left-right arrow to move the Stock.symbol field to the on change of field.

· Select Worth and click the right arrow to move the worth formula to the show Value (s) field.

· Click the Text tab.

· Next to title, clear the Auto-text check box.

· Enter Worth/symbol in the Title field.

· Click OK.

· A new table header section is created, and a chart object is added to this section.

· To reposition the objects in a Crystal report, you can drag them with the mouse and place them wherever you want to put them. You can use the "Main report Preview" button at the bottom of the form to display a preview of your reports.

3. Add the formula and summary fields to your report. In this section, you add a formula field to your report and a summary field-it calculates the total value of your portfolio.

· Expand the "Formula Fields" node of field Explorer.

· Drag the worth formula to your report. Put this field in the section of your report.

· If "Field Explorer" is not visible, select "Document Outline" from the View menu. This field will display the value of each row. Use a summary field to display the total value of your portfolio.

· From the "Crystal Reports" menu, select "Insert" and click "Summary".

· The Insert Summary dialog box appears.

· Select the worth formula from the "Choose to summarize" field.

· From the Calculate this Summary field, select Sum.

· Select Grand total from the Summary Location field.

· Click OK.

Add a summary field to the report.

· From the "Debug" menu, click "Start Debugging".

· If no build errors occur, the Default.aspx page will be loaded into your browser.

· Close the Internet Explorer window.

4. Add a previous report that already exists. In this section, you will have your Web application use an existing report located in your file system.

· Right-click the Stockobjects.rpt in Solution Explorer.

· Click Delete.

· Click OK in the pop-up dialog box.

· In Solution Explorer, right-click the bold Web site name and click "Add Existing Item".

· In the ADD Existing Item dialog box, navigate to C:\Microsoft hands-on-lab\hol-ill05\source\excercises and select Stockobjects.rpt file.

· Click Add.

· From the Debug menu, click "Start Debugging".

· If there is no build error, default.aspx page will load into your browser and display your new report.

· Close the Internet Explorer window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.