Main Page

  Previous Section Next Section

14.3 Collections Reference

The Context object in ASP.NET supports one collection, Items, and an array called AllErrors.


ExceptionArray = Context.AllErrors

Returns an array of Exception objects representing all accumulated errors that occurred in the current request.

As in classic ASP, the Server.GetLastError method returns an ASPError object. This mechanism is still available, though the returned value is now of type Exception rather than ASPError.




The example checks to see if the AllErrors array contains any elements and if so, displays them:

Sub Page_Load( )
    Dim i as Integer
    Dim e As New Exception("A generalized error.")
    If Not Context.AllErrors Is Nothing Then
        For i = 0 to Context.AllErrors.Length - 1 
            Message.Text = Message.Text & _ 
                "Exception: " & _
                Context.AllErrors(i).ToString( ) & "<br/>"
        Message.Text = "No Errors to report."
    End if
End Sub


Unlike classic ASP, arrays in ASP.NET are zero-based, so the first element in any collection or array will be 0, not 1. Thus, in the example above, the array is indexed from 0 to Length - 1, not from 1 to Length.


Context.Items(Name as String) = Value
Value = Context.Items(Index as Integer)
Value = Context.Items(Name as String)

The Items collection is a key-value collection that can contain any object the developer wishes to save for, at most, the duration of the request. Unlike Session- or Application-level collections that can be used to store values, this collection does not survive past the current request. This collection is the ideal place to store values that need not survive past the current request, especially if the items need to be stored or retrieved in places where the Session or Application objects are not available.



The key name for the value to be stored.


The object to be stored in the Items collection, or the value to be retrieved.


The index of the value to be retrieved.


The example adds two values to the Context.Items collection. The first is added traditionally, referring to the key in the Items collection directly. The second is added using the Item's collection's Add method. Finally, the Message label control displays whether the "Foo" value has been set. To display the string key used, you need to use two double quotes, which displays as the literal quote character. In C#, the quote character would need to be escaped by placing a backslash (\) in front of it.

Sub Page_Load( )
   Message.Text = "Context.Items.Contains(""Foo"") is " & _
End Sub


Unlike classic ASP, collections in ASP.NET are zero-based, so the first element in any collection or array is 0, not 1. While you can, as in the example above, use the Add method to add values to the Items collection, this is virtually never done. Values are almost always retrieved by referring to the Items collection directly, either by using a numeric index or indexing using a key string.

      Previous Section Next Section