How to set and retrieve a session variable for a current Web Form in ASP.NET - A simple example

[Migrated content. Thread originally posted on 04 February 2011]

In Visual COBOL R3:


Setting a value

$ilusing "System.Web"
$ilusing "System"

01 myString String.
...
...
set myString to textBox1::Text
set Self::Session::Item("VarKey1") to myString


Retrieving a Value:

$ilusing "System.Web"
$ilusing "System"


01 myString String.
...
...
try
    set myString to Self::Session::Item("VarKey1")
catch
    set LabelStatus::Text to exception-object::Message
end-try

NOTE: The Visual COBOL R3 example makes use of a built in exception-object that is automatically generated to catch an exception in a Try-Catch block.



In NetExpress for .NET:

Setting a Value

$ilusing "System.Web"
$ilusing "System"

01 myString String.
...
...

set myString to textBox1::"Text"
set Self::"Session"::"Item"("VarKey1") to myString


Retrieving a Value:

$ilusing "System.Web"
$ilusing "System"

01 myString String.
01 NullEx Type "NullReferenceException".
...
...
try
    set myString to Self::"Session"::"Item"("VarKey1") 
catch NullEx
    set LabelStatus::"Text" to "Entry Not Found..."
end-try