Crystal Reports for Visual Studio 2005 in CSharp - Exercise 5: Adding Data Dynamically to the Stock Report (Page 4 of 5 ) Scenario In the previous exercise, you populated the Object Collection programmatically. In this exercise, you will learn how to dynamically add information to your data source from your website. This information will automatically update in your report. Tasks Detailed Steps - Adding Controls to the Web Form.
- Open the Default.aspx file in Design View.
To open an ASPX page in design view, first open the file, and then click the Design button at the bottom of the form.
- Click the CrystalReportViewer control to select it.
- Press the left arrow key on your keyboard so that the flashing cursor appears, and the press Enter to move the viewer down the page four times.
From the Toolbox drag a TextBox control onto the web form.
From the Property menu, set the ID to symbol.
Drag a second TextBox control onto the web form. Position the second TextBox below the first.
From the Property menu, set the ID to price.
Drag a third TextBox control onto the web form. Position the third TextBox below the second.
From the Property menu, set the ID to volume.
At this point, you may find it useful to add text beside each TextBox control to help identify which control corresponds to which parameter.
Next, from the Toolbox, drag a Button control onto the web form. Place the button below the three TextBox controls.
From the Property menu set the ID of the Button to addStockInformation.
Set the Text of the Button to Add Stock Information.
Finally, double click on the Add Stock Information button.
Double clicking on the Button control will open up the Code-behind class and automatically generate an addStockInformation_Click() event handler.
Adding Information to the Collection.
Inside of the addStockInformation_Click() event handler, create and instantiate a new Stock object.
Stock temp = new Stock();
Within the addStockInformation_Click() method, create a try/catch block.
try { } catch { }
Information entered into a web form is of type String. Because two of the fields within the Stock class are numerical, you will need to write code to convert the String values from the web form into numerical values. The try/catch statement helps protect your web application from crashing during the conversion due to type mismatch errors.
Within the Try block, assign the value of the symbol field on the web form to the Symbol property of the Stock Object.
temp.Symbol = symbol.Text;
On the next line, assign the value of the price field on the web form to the Price property of the Stock Object. Convert the value from the web form to a Double before assignment.
temp.Price = Convert.ToDouble(price.Text);
Next, assign the value of the volume field on the web form to the Volume property of the Stock Object. Convert the value from the web form to an integer before assignment.
temp.Volume = Convert.ToInt32(volume.Text);
Outside of the try/catch block, add the Stock Object to the stockValues ArrayList.
stockValues.Add(temp);
Update the value of stockValues currently held in Session.
Session["stockValues"] = stockValues;
Finally, call the ConfigureCrystalReports() method. This will re-bind the report to the updated stockValues Object Collection.
ConfigureCrystalReports();
From the Build menu, click Build Solution.
If you have any build errors, fix them now.
From the Debug menu, click Start Debugging.
If no build errors appear, the Default.aspx page loads into your browser with three default values. To add additional values, fill in the TextBox Controls as appropriate and click the Add Stock Information button. The Report will dynamically update.
When you run your web site, the report will load in your browser window with the three default values that you programmatically added in Exercise 4 Binding your Crystal Report to the Crystal Report Viewer. Above the report are three TextBox controls and a Button control. With these controls, you can dynamically update your Object Collection, and see the result of this update reflected in your report.
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 6: Adding Charts and Summary Information to the Report >>
More C# Articles More By MSDN Virtual Labs |