Search the articles  
  

• Using Cookies variables with Class

Tuesday, July 28, 2009

This method is very similar to the "USING SERVER SIDE VARIABLES WITH CLASS" article except using client side cookies.

For example,
In order to maintain Order ID in shopping cart,
Response.Cookies("ORDERID").Value method can be used but it is truly unwise to reuse the same variable name again and again every time when you need to get the Order ID value.

What if you need to change the "ORDERID" variable name to "$_ORDERID_$", you will have to find and replace all the same variable in the files.

What if you need to apply encryption/decryption method to the sensitive data storing in Session variables, again you will have to find and apply the method to all the same variable in the files.

In order to avoid such a hassle method looking for the same variable in every places, this is how I always practice using and referencing the server side variables with class.

Create a new class, OrderID.cs, file in your project.
The file structure will be looked like this.


Inside the OrderID.cs, copy and place these codes.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MyCookies
{
public class OrderID
{
//Assign the Order ID value into to current HTTP Cookie variable OrderID
public static string Set
{
set { HttpContext.Current.Response.Cookies["OrderID"].Value = value; }
}

//Get the Order ID value from current HTTP Cookie variable OrderID
public static string Get
{
get { return GetString(HttpContext.Current.Request.Cookies["OrderID"].Value); }
}

//Delete Cookie variable OrderID from current HTTP
public static void Clear()
{
HttpContext.Current.Response.Cookies["OrderID"].Expires = DateTime.Now;
}

//Get the Order ID value from current HTTP Cookie variable OrderID
public static DateTime SetExpiration
{
set { HttpContext.Current.Request.Cookies["OrderID"].Expires = value; }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//This GetString Method should not be in this class,
//This method should put into seperate class which hold a collection of getting standard data type
//////////////////////////////////////////////////////////////////////////////////////////////////////
public static string GetString(object value)
{
try
{
return Convert.ToString(value);
}
catch
{
return String.Empty;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
}
}



Now, you are ready to start using Cookie variable through the class.

To assign your "OrderID" to the session variable, just simple assign as below
MyCookies.OrderID.Set = "123";

To get your "OrderID" from session variable,
############## = MyCookies.OrderID.Get;

To clear the "OrderID" from session variable when it is not longer used.
MyCookies.OrderID.Clear();

To set the cookie expiration for 24 hours.
MyCookies.OrderID.SetExpiration = DateTime.Now.AddHours(24);


No comments:

Post a Comment