Inmemory Cookies, Persistant Cookies in Asp.Net

Cookies: 

1)website storing a variable with in client system is called “cooki variable” 

2)cookies can be used to maintain user information across different webpages with in website by storing with in client system 

3)the advantage with cookies is reducing memory burden on webserver system by storing variables with in client system

 

Cookies can be classified into 2 types 

1.inmemory cookie 

2.persistant cookie

 

Inmemory cookie:

Cookie variable stored with in browser process memory is called “inmemory cookie”. 

Inmemory cookie is a temporary cookie closing browser will erase cookie variables.

 

Creating cookie variable: 

Response.cookies[“varname”].value=”…”;

 

->cookie variable can store only string type data 

->cookie variable can store maximum of 4kb data

 

Reading cookie variable: 

Reading cookie is possible based on name (or) index 

EX:Request.cookies[“varname”].value 

             (or) 

     Request.cookies[index].value

 

Creating online examination website using inmemory cookie concept 

Goto visual studio 

Start->run->devenv 

It will display main window of visual studio 

File menu->new->website->visual c#->select asp.net empty website 

Weblocation->e:\aspnet\onlineexamsite[drive:\dir\websitename]

 

Visual studio create a folder with website name, in this folder website related files will be placed

 

Add webform 

Goto view menu and select solution explorer 

Right click on website path and select add new item 

Select webform 

Give name as question1.aspx 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="question1.aspx.cs" Inherits="question1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/html1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

    <title></title> 

</head> 

<body> 

    <form id="form1" runat="server"> 

    <div>

    </div> 

    </form> 

</body> 

</html>

 

Goto go to design part

 

What is CLR[label1] 

  Common language runtime 

  Common language resource 

next

 Radiobutton1 properties 

Id-r1 

Groupname-g1

 

Radiobutton2 properties 

Id-r2 

Groupname-g1

 

//next button click logic

 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class question1 : System.Web.UI.Page 

{ 

    protected void Page_Load(object sender, EventArgs e) 

    {  

    }

 

    protected void Button1_Click(object sender, EventArgs e) 

    { 

        if (r1.Checked) 

            Response.Cookies["q1"].Value = "y"; 

        else 

            Response.Cookies["q2"].Value = "n";

 

        Server.Transfer("question2.aspx"); 

    } 

}

 

Add webform 

Goto view menu and select solution explorer 

Right click on website path and select add new item 

Select webform 

Give name as question2.aspx 

Goto design part 

What is CTS[label1] 

  Common transper system 

  Common type system 

next

Radiobutton1 properties

Id-r1 

Groupname-g1

 

Radiobutton2 properties 

Id-r2 

Groupname-g1 

 

//next button click logic

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class question2 : System.Web.UI.Page 

{ 

    protected void Page_Load(object sender, EventArgs e) 

    { 

    }

 

    protected void Button1_Click(object sender, EventArgs e) 

    { 

        if (r2.Checked) 

            Response.Cookies["q2"].Value = "y"; 

        else 

            Response.Cookies["q2"].Value = "n";

 

        Server.Transfer("question3.aspx"); 

    } 

}

 

Add webform 

Goto view menu and select solution explorer 

Right click on website path and select add new item

Select webform 

Give name as question3.aspx  

What is GAC[label1] 

  Global assembly cache 

  Global assembly center

 

ok

Radiobutton1 properties 

Id-r1 

Groupname-g1

 

Radiobutton2 properties 

Id-r2 

Groupname-g1

 

//ok button click logic

 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class question3 : System.Web.UI.Page 

{ 

    protected void Page_Load(object sender, EventArgs e) 

    { 

    }

 

    protected void Button1_Click(object sender, EventArgs e) 

    { 

        if (r1.Checked) 

            Response.Cookies["q3"].Value = "y"; 

        else 

            Response.Cookies["q3"].Value = "n";

 

        Button1.Visible = false; 

        HyperLink1.Visible = true; 

    } 

}

 

Add webform 

Goto view menu and select solution explorer 

Right click on website path and select add new item 

Select webform 

Give name as result.aspx 

Place label control

 

//page load event logic

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class result : System.Web.UI.Page 

{ 

    protected void Page_Load(object sender, EventArgs e) 

    { 

        string s = ""; 

        string v = ""; 

        int count = 0;

 

        for (byte i = 0; i < Request.Cookies.Count; i++) 

        { 

            v = Request.Cookies[i].Value; 

            s = s + "q1" + (i + 1) + "." + v + "<br>"; 

            if (v == "y") 

                count = count + 1; 

        }

 

        Label1.Text = "result sheet <br>" + s + "total correct:" + count; 

    } 

} 

 

set start page as question1.aspx 

goto contrl F5

 

Note: 

browser submitted cookie variables will be stored with in request object

 

Persistant cookie: 

->Storing cookie variable with in textfile at client system is called “persistant cookie” 

->persistant cookie will be maintained with in client system based on particular lifetime 

->creating persistant cookie 

    Response.cookies[“varname”].value=”..”; 

    Response.cookies[“varname”].expires=datetime.now.adddays(2); 

->datetime.now returns current system date and time 

->adddays method can be used to add number of days to system date 

->similarly u can go with addminutes, add hours, add years….