Wednesday, January 30, 2008

State Management in ASP.NET

Web form pages are HTTP-Based, they are stateless, which means they don’t know whether the requests are all from the same client, and pages are destroyed and recreated with each round trip to the server, therefore information will be lost, therefore state management is really an issue in developing web applications. We could easily solve these problems in ASP with cookie, query string, application, session and so on. Now in ASP.NET, we still can use these functions, but they are richer and more powerful, so let’s dive into it.

Mainly there are two different ways to manage web page’s state

  • Client-side

  • Server-side

Client-side state management

There is no information maintained on the server between round trips. Information will be stored in the page or on the client’s computer.

1) Cookies

A cookie is a small amount of data stored either in a text file on the client's file system or in-memory in the client browser session. Cookies are mainly used for tracking data settings. Let’s take an example: say we want to customize a welcome web page, when the user request the default web page, the application first to detect if the user has logined before, we can retrieve the user information from cookies.

//to create a cookie variable
Response.Cookies["username"].Value=name;

//to access a cookie variable
Request.Cookies["username"].Value

2) Hidden Field

A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page. ASP.NET provides the HtmlInputHidden control that offers hidden field functionality.

protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;//to assign a value to Hidden fieldHidden1.Value=”this is a test”;//to retrieve a value string str=Hidden1.Value;

Note: Keep in mind, in order to use hidden field, you have to use HTTP-Post method to post web page. Although its name is ‘Hidden’, its value is not hidden, you can see its value through ‘view source’ function.

C. View State
Each control on a Web Forms page, including the page itself, has a ViewState property, it is a built-in struture for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server. Here, which is useful to us is the ViewState property, we can use it to save information between round trips to the server.

//to save informationViewState.Add(“shape”,”circle”);//to retrieve informationstring shapes=ViewState[“shape”];

Note: Unlike Hidden Field, the values in ViewState are invisible when ‘view source’, they are compressed and encoded.
D.Query Strings
Query strings provide a simple but limited way of maintaining some state information.
You can easily pass information from one page to another, But most browsers and client devices impose a 255-character limit on the length of the URL. In addition, the query values are exposed to the Internet via the URL so in some cases security may be an issue.

This is an article in its entirity has been posted on http://www.csharphelp.com/. I am reposting this on my blog since I feel it is very useful.

Reference:
1. http://www.csharphelp.com/archives/archive207.html

STANDARD TEMPLATE LIBRARY (STL)

CONTAINERS

String, List, Vector , Hash, Map, Multimap, Set,

String - #include
A string is a collection of ascii characters that supports both insertion and removal.

List vs Vector - #include "list" vs #include "vector"

Both of them can store collection of objects and work almost similarly.

Lists are double-linked lists. Where as a vector is sequential in memory like an array. Insertion at either end, or in the middle (providing you know an existing element in the middle) is equally fast. Also, splicing is fast (eg. putting a list into the middle of another) - all that does is re-arrange the end pointers. All of those operations would be slower with vectors, because you would need to copy everything. However, vectors shine in random access - since they are organised sequentially in memory, whilst this is slow for insertion at anywhere other than the end, you can easily find, say, element 500, you simply index into the vector. Thus, vectors can be sorted, shuffled, and accessed randomly.

Map - #include "map"
A collection for a varying length sequence of elements of type pair. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element. The key can be a simple as a number or string or as complex a key class. The key class must support normal comparison operations so that the collection can be sorted or searched.

Set - #include "set"
A collection that controls a varying length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.

Multimap - #include "map"
A collection of a varying length sequence of elements of type pair. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.

Multiset - #include
A collection of a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.


ALLOCATORS


Iterators

Iterators support the access of elements in a collection. They are used throughout the STL to access and list elements in a container. The iterators for a particular collection are defined in the collection class definition.

Monday, January 28, 2008

C# Copy Constructor

C# does not provide a copy constructor by default. We have to write an appropriate constructor method that will create a new object and to copy the values from an existing object. This implementation is nothing like the C++ copy constructor.

If we need a copy of an object we can use cloning techniques.

Observation:
This method has to be called explicitly.

Note: "A shallow copy creates a new instance of the same type as the originalobject, and then copies the non-static fields of the original object. If thefield is a value type, a bit-by-bit copy of the field is performed. If thefield is a reference type, the reference is copied but the referred objectis not; therefore, the reference in the original object and the reference inthe clone point to the same object. In contrast, a deep copy of an objectduplicates everything directly or indirectly referenced by the fields in theobject."

References:
1. MSDN Online

Generic Sql Table Data Insert Script

woah!!

I have my own blog and this is my first posting. hurray.... I have been shying away from doing this for a long time now... finally pulled all my energies to start blogging tech stuff that I learn ...

There is so much that I learn and so much I forget all the time.... It is getting really tough to keep track of all that I have learned.... I hope I can keep up with posting useful stuff that I learn.

enough of my preamble now... cutting the chase and getting to my post...

How many time must I have thought " I wish I had a script that I would run with the table name as an argument and get insert statements for all the data in a Sql Server 2000/2005 table." Thanks to Narayana Vyas Kondreddi, I have got a script [1] that does exactly what I want. This is a cool utility that is very handy.

To generate INSERT statements for table 'titles':
EXEC sp_generate_inserts 'titles'


References:
1. http://vyaskn.tripod.com/code.htm#tagit