This documentation is archived and is not being maintained.

Populating the Object Collection Dynamically

Visual Studio 2005

In the previous section, you populated the Object Collection programmatically. In this section, you will learn how to dynamically add information to your data source from your website. This information will automatically update in your report.

To Add Controls to the Web Form

In this section, you add the controls to the Web Form that are needed to dynamically update the Object Collection.

  1. Open the Default.aspx file in Design View.
    Note To open an ASPX page in design view, first open the file, and then click the Design button at the bottom of the form.
  2. Click the CrystalReportViewer control to select it.
  3. Press the left arrow key on your keyboard so that the flashing cursor appears, and the press enter several times.
  4. From the Toolbox drag a TextBox control onto the web form.
  5. From the Property menu, set the ID to "symbol."
  6. Drag a second TextBox control onto the web form. Position the second TextBox below the first.
  7. From the Property menu, set the ID to "price."
  8. Drag a third TextBox control onto the web form. Position the third TextBox below the second.
  9. From the Property menu, set the ID to "volume."
    Note At this point, you may find it useful to add text beside each TextBox control to help identify which control corresponds to which parameter.
  10. Next, from the Toolbox, drag a Button control onto the web form. Place the button below the three TextBox controls.
  11. From the Property menu:
    • Set the ID of the Button to "addStockInformation."
    • Set the Text of the Button to "Add Stock Information."
  12. Finally, click twice 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.

To Add Information to the Collection

In this section, you will write code for the addStockInformation_Click() event handler that will add the information entered in the Web Form to the stockValues collection.

  1. Inside of the addStockInformation_Click() event handler, create and instantiate a new Stock object.
    [Visual Basic]
    Dim temp As Stock = New Stock()
    Stock temp = new Stock();
  2. Within the addStockInformation_Click() method, create a try/catch block.
    [Visual Basic]
    Catch ex As Exception
    End Try 

    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.

  3. Within the Try block, assign the value of the symbol field on the web form to the Symbol property of the Stock Object.
    [Visual Basic]
    temp.Symbol = symbol.Text
    temp.Symbol = symbol.Text;
  4. 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.
    [Visual Basic]
    temp.Price = CType(price.Text, Double)
    temp.Price = Convert.ToDouble(price.Text);
  5. 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.
    [Visual Basic]
    temp.Volume = CType(volume.Text, Integer)
    temp.Volume = Convert.ToInt32(volume.Text);
  6. Outside of the try/catch block, add the Stock Object to the stockValues ArrayList.
    [Visual Basic]
  7. Update the value of stockValues currently held in Session.
    [Visual Basic]
    Session("stockValues") = stockValues
    Session["stockValues"] = stockValues;
  8. Finally, call the ConfigureCrystalReports() method. This will re-bind the report to the updated stockValues Object Collection.
    [Visual Basic]
  9. From the Build menu, click Build Solution.

    If you have any build errors, fix them now.

  10. 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 Populating the Object Collection Programmatically. 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.

Continue to Conclusion.

See Also

Connecting to Object Collections | Tutorials and Sample Code | Tutorials' Sample Code Directory