Export (0) Print
Expand All

Populating the Object Collection Programmatically

Visual Studio 2005

In this section, 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.

  1. Right click on the web form in the Solution Explorer and click View Code.
  2. Within the class, add a new public scope helper method, with no return value, named PopulateStockValuesArrayList().
    [Visual Basic]
    Public Sub PopulateStockValuesArrayList()
    
    End Sub
    [C#]
    public void PopulateStockValuesArrayList()
    {
    }
  3. Within the PopulateStockValuesArrayList() method, before the existing code, create an if/else conditional block that checks whether a Session object named stockValues exists.
    [Visual Basic]
    If (Session("stockValues") Is Nothing) Then
    
    Else
    
    End If
    [C#]
    if(Session["stockValues"] == null)
    {
    }
    else
    {
    }
  4. Within the If block, instantiate a new ArrayList()
    [Visual Basic]
    stockValues = New ArrayList
    [C#]
    stockValues = new ArrayList();
  5. Next, use the overloaded constructor for the Stock class to create and instantiate three instances of Stock.
    [Visual Basic]
    Dim s1 As Stock = New Stock("AWRK", 1200, 28.47)
    Dim s2 As Stock = New Stock("CTSO", 800, 128.69)
    Dim s3 As Stock = New Stock("LTWR", 1800, 12.95)
    [C#]
    Stock s1 = new Stock("AWRK",1200,28.47);
    Stock s2 = new Stock("CTSO",800,128.69);
    Stock s3 = new Stock("LTWR",1800,12.95);
  6. Add these three instances to stockValues.
    [Visual Basic]
    stockValues.Add(s1)
    stockValues.Add(s2)
    stockValues.Add(s3)
    [C#]
    stockValues.Add(s1);
    stockValues.Add(s2);
    stockValues.Add(s3);
  7. Add the updated stockValues ArrayList into session.
    [Visual Basic]
    Session("stockValues") = stockValues
    [C#]
    Session["stockValues"]=stockValues;
  8. Inside of the Else block, write a line to assign the current values held in session to the stockValues ArrayList.
    [Visual Basic]
    stockValues = Ctype(Session("stockValues"), ArrayList)
    [C#]
    stockValues = (ArrayList)Session["stockValues"];
  9. Finally, call the PopulateStockValuesArrayList() from the ConfigureCrystalReports() method. This should be the first line of code executed in the ConfigureCrystalReports() method.
    [Visual Basic]
    PopulateStockValuesArrayList()
    [C#]
    PopulateStockValuesArrayList();

To test the loading of the StockObjects report

  1. From the Build menu, select Build Solution.
  2. If you have any build errors, go ahead and fix them now.
  3. From the Debug menu, click Start Debugging.
    Note   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.

    If no build errors appear, the Default.aspx page loads into your browser with three default values.

  4. Return to Visual Studio and click Stop to exit from debug mode.

Continue to Populating the Object Collection Dynamically.

See Also

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

Show:
© 2014 Microsoft