Monday, February 25, 2008

Keep that session alive

Ever came across a situation that demands a long lasting session, even if you have no activity on the client browser.

There is a pretty simple workaround for this problem. All you need to do is to call a page from server just before your session expires. This will simply refresh your session and it wil enter another cycle.

 

Now if you open a popup using Javascript (function scheduled with setInterval() or setTimeout()), the user may not feel comfortable. So you can have a blank page on server and call it though AJAX. For more details on AJAX, check http://vikramjits.blogspot.com/2007/09/ajax-with-html.html

Now, you need a function in your page that calls your ajax function (which inturn calls a blank page on server, and do not bother to populate it anywhere). This function is to be called every few seconds/minutes (based on the Session Timout Interval set in your application. Ideally you can set it to 30 seconds.

Wednesday, February 13, 2008

.NET AJAX (ATLAS) Problem : The state information is invalid for this page and might be corrupt

I did lot of R&D and checked several sites for this. Finally I got few leads. This problem revolves around the VIEWSTATE of application. I checked the BLOG of Syed Ghulam Akbar that talks about the size of Viewstate. It suggests that you remove the size limitation of VIEWSTATE to solve the problem. A comment was there in his blog that said that following script can help resolve the problem...

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoading(function() {
var f = document.getElementById(“__VIEWSTATEFIELDCOUNT”);
if (f) f.parentNode.removeChild(f);
});

 

Another BLOG that I checked suggested that change the CACHE settings so that browsers like Firefox do not generate this error. Command Response.Cache.SetNoStore() is the quickest work-around. Or you can manage this in the Page Declaration by using...

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="account.aspx.cs" Inherits="Main" Title="My Account" EnableViewStateMac ="false" EnableSessionState="True" EnableEventValidation ="false" ValidateRequest ="false" ViewStateEncryptionMode ="Never" %>

Or by changing the WEB.CONFIG file...

<pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode ="Never">

Remember, if you are doing this WEB.CONFIG change, than you should check with this validateRequest parameter. Like in my case, I was having some HTML content being entered by user, so have to explicitly enable validateRequest on selected pages.

 

References:

http://syedgakbar.wordpress.com/2007/11/08/one-possible-reason-for-%E2%80%9Cthe-state-information-is-invalid-for-this-page%E2%80%A6%E2%80%9D-exception/

http://renditionprotocol.blogspot.com/2007/01/state-information-is-invalid-for-this.html

How to swap numbers

Some student asked me to help him with this program to swap two numbers. So I thought it might be good to post it here as well...

 

There are two methods of swapping 2 numbers.

1. Using a third temporary variable

2. Without using a third variable

 

Let's discuss both of them...

By using a temporary variable

Say you need to swap Var1 and Var2, then create a third variable (say Var3) and use following syntax...

Var3 = Var2

Var2 = Var1

Var1 = Var3

 

See, it's easy. Now let's talk about a little tricky method...

Without Using a temporary Variable

If you have to swap Var1 and Var2, use the following technique...

Var1 = Var1 + Var2

Var2 = Var1 - Var2

Var1 = Var1 - Var2

 

Have fun

Tuesday, February 05, 2008

Normalization

There are two types of popular Normalization techniques.

Star Normalization and Snowflake Normalization

 

Star Normalization

In Star Normalization, different interrelated tables refer to a single common table containing relationship of all tables. This is a very benifitial technique as during query writing, you need not to join too many tables to reach desired data.

 

Snowflake Normalization

Snowflake normalization refers to a technique of normalization where one table’s column refers to the other columns primary key, which in turn refers to the third one, thus creating a snowflake like structure. In such structure, whenever the data from lowest in hierarchy is required to be grouped with top level table, all tables are to be traversed, which create unnecessary load on the data server.

Normalization

 

ALWAYS USE STAR NORMALIZATION WHILE DESIGNING DATABASE