Populating the Object Collection Programmatically
Right click on the web form in the Solution Explorer and click View Code.
Within the class, add a new public scope helper method, with no return value, named PopulateStockValuesArrayList().
``` vb
Public Sub PopulateStockValuesArrayList()
End Sub
```
``` csharp
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.
``` vb
If (Session("stockValues") Is Nothing) Then
Else
End If
```
``` csharp
if(Session["stockValues"] == null)
{
}
else
{
}
```
- Within the If block, instantiate a new ArrayList().
``` vb
stockValues = New ArrayList
```
``` csharp
stockValues = new ArrayList();
```
Next, use the overloaded constructor for the Stock class to create and instantiate three instances of Stock.
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)
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.
``` vb
stockValues.Add(s1)
stockValues.Add(s2)
stockValues.Add(s3)
```
``` csharp
stockValues.Add(s1);
stockValues.Add(s2);
stockValues.Add(s3);
```
Add the updated stockValues ArrayList into session.
Session("stockValues") = stockValues
Session["stockValues"]=stockValues;
Inside of the Else block, write a line to assign the current values held in session to the stockValues ArrayList.
``` vb
stockValues = Ctype(Session("stockValues"), ArrayList)
```
``` csharp
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.
``` vb
PopulateStockValuesArrayList()
```
``` csharp
PopulateStockValuesArrayList();
```