Showing posts with label HTTP. Show all posts
Showing posts with label HTTP. Show all posts

Tuesday, March 1, 2011

Sunday, February 6, 2011

Session Variables

Requests between servers and browsers are handled via HTTP - Hypertext Transfer Protocol.

HTTP is a stateless protocol. This means that each request is serviced as it comes; after the request is processed, all of the data is discarded. No state is maintained across requests even from the same client. In a nutshell, data from one page is generally unavailable on the next page etc.

When we create a Desktop Application, often we use variables to hold data that persists for the life of the program execution. There are several ways to achieve the same result on the ASP.NET platform. One way is to use Session Variables.

A Session Variable can be read from anywhere in your website. You can use them to store information for the lifetime of a single users visit. Session variables do not need to be declared, they can be written on the fly.

Session("SessionName") = value

That's not to say that we have stopped using variables, not at all. But in the ASP.NET environment we can no longer rely on them to store application wide data.

Checking the Session Exists

Sessions Time-out if the user is idle for a period of time. This period of time is variable from server to server but generally it is a matter of only a few minutes. Because of this, we should always check if the session exists before we attempt to reference one.

If Session.Item("CustomerID") IsNot Nothing Then
            Response.Write(Session("CustomerID"))
Else
            Response.Write("Session CustomerID does not exist")
End If

Response, Request & Query Strings

Requests between servers and browsers are handled via HTTP - Hypertext Transfer Protocol.

HTTP is a stateless protocol. This means that each request is serviced as it comes; after the request is processed, all of the data is discarded. No state is maintained across requests even from the same client. In a nutshell, data from one page is generally unavailable on the next page etc.

Response is a property of the HttpResponse object. This represents the outgoing data from the current Web form object. We can use the Redirect method to navigate to a new Web form. 

response.redirect("nextpage.aspx")

Request is a property of the HttpRequest object. This represents incoming data to the current Web form. The information stored in the Request object collections originates from the client

We can read the properties of the HttpRequest object to learn more about the client and the page currently being viewed.

lblURL.Text = Request.Url.ToString 

Use Query Strings to send data to the next page

A Query String is composed of field-value pairs and is appended to a URL.

response.redirect("pagename.aspx?FieldName=Value")

The above URL contains a querystring containing a single name/value pair.

Here is another example:

response.redirect("pagename.aspx?FieldName1=Value1&FieldName2=Value2")

Query Strings are useful for passing non sensitive data between 2 pages. The querystring begins with the question mark (?) character. Each name value pair is delineated by an ampersand (&) character.

When using the concatenation character to build a querystring, you need to really pay attention to the ampersand as you are using it in 2 contexts:

1) as the delineator in the querystring
2) as the concatenation operator in the string as it is being built

For instance:


In the above querystring, there are 3 name/value pairs. Each value for each name, is supplied by a string variable. The string is built using the concatenation operator - the ampersand, however, each subsequent name/value pair is also separated by the ampersand character. Concentrate & you will be fine (see what I did there?). LOL.

Read Query Strings on the next page

We can use the request object to read the values of the fields within the Query String.

Dim X as String = Request.QueryString("FieldName")


or as per the above example:


Dim strFirstName as String = Request.QueryString("Firstname")
Dim strLastName as String = Request.QueryString("Lastname")
Dim strPostCode as String = Request.QueryString("PostCode")



The following series of screenshots show values from textboxes being concatenated into a querystring & then displayed on the following page:

1.

 2.

 3.

 4.

 5.

Get a copy of the project here :))