Friday 27 December 2013

What is State Management technique in Asp.Net (Cookies)

What is State Management technique in Asp.Net

State Management is the process by which we maintain session related information and
Additional information about the controls and its state. The necessity of state management
arises when multiple users request for the same or different Web Pages of a Web Site.
When users visit Web sites it becomes necessary to maintain session related and controls
related information. In an HTTP exchange between a browser and a remote host, session
related information which identifies state, such as a unique session ID, information about the user's preferences or authorization level is preserved. Note that sessions are maintained in the data being exchanged.

There are two type techniques in state management in Asp.Net

·         Client Side State Management
   
A.    Cookies
B.    Query String
C.    Hidden Field
D.   View State

·         Server Side State Management

A.    Session
B.    Profile
C.    Application
D.   Cache

Cookies: Cookies is text file use to store the data as client machine. It is stored in browser

·         Maximum data are sent or store in a cookies are 4 kb and only string type
·         Cookies are the object in the Asp.Net. It is a text file which is sent by the server to browser
·         One website have store 20 cookies for a Website
·         Cookies are created on server but stores in client Browser , cookies sends to server with Request and the cookies comes  on client with Response Method
·         By default  time  of Cookies is  14  days

Type of cookies in Asp.net: There are two type cookies in Asp.Net
  
·         Persistent Cookies
·         Non Persistent Cookies

Persistent Cookies: It data stored in permanent location or hard disk this type cookies is known as persistent cookies  .It is  machine specific or computer specific. By this concept cookies stored in browser

Example:
         

protected void btnCreateCookies_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("mycookies");
ck.Value = txtname.Text;
ck.Expires = DateTime.Now.AddSeconds(300);
Response.Cookies.Add(ck);
Response.Redirect("Default2.aspx");
}

// How to get value of Cookies on another Page

protected void btngetcookies_Click(object sender, EventArgs e)
{
HttpCookie ck = Request.Cookies["mycookies"];
Response.Write(ck.Value);
}


Non-Persistent Cookies: It data stored in temporary  location or computer RAM  this type cookies is known as non-persistent cookies  .It is  machine specific or computer specific.

Example:
         

protected void btnCreateCookies_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("mycookies");
ck.Value = txtname.Text;
Response.Cookies.Add(ck);
Response.Redirect("Default2.aspx");
}

// How to get value of Cookies on another Page

protected void btngetcookies_Click(object sender, EventArgs e)
{
HttpCookie ck = Request.Cookies["mycookies"];
Response.Write(ck.Value);
}


Practices with Cookies Concept……………………………………………….

Note: In this concept we create to single cookies and get its values

protected void btnCreateCookies_Click(object sender, EventArgs e)
{

HttpCookie ck1 = new HttpCookie("fname");
ck1.Value =txtfirstname.Text;
ck1.Expires = DateTime.Now.AddDays(14);
Response.Cookies.Add(ck1);

HttpCookie ck2 = new HttpCookie("lname");
ck2.Value =txtlastname.Text;
ck2.Expires = DateTime.Now.AddDays(14);
Response.Cookies.Add(ck2);



}

protected void btnGetCookies_Click(object sender, EventArgs e)
{
HttpCookie ch1 = Request.Cookies["fname"];
HttpCookie ch2 = Request.Cookies["lname"];
Response.Write("Name" + ch1.Value + "  " + ch2.Value);

// get all cookies with foreach
foreach (string s in Request.Cookies)
{
Response.Write(Request.Cookies[s].Value + "<br/>");
}

}
                                                                                                                      
Note: In this concept we create to multivalue cookies and get it values


//create  multivaluedCookies in Asp.Net

protected void btnCreateCookies_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("userinfo");
ck.Values["fname"] = txtfirstname.Text;
ck.Values["lname"] = txtlastname.Text;
ck.Expires = DateTime.Now.AddSeconds(300);
Response.Cookies.Add(ck);
}

//How to get value of multivaluedCookies

protected void btnGetCookies_Click(object sender, EventArgs e)
{
HttpCookie ck = Request.Cookies["userinfo"];
if (ck.HasKeys)
{
Response.Write(ck["fname"] + "," + ck["lname"]);
}
// using foreach loop………………………………..
foreach (string s in ck.Values.AllKeys)
{
Response.Write(ck[s] + "<br/>");
}
}
Note: single valued cookies take 50 characters but multi valued cookies also take 50 characters on client(Browser) at  one  time………..


HttpCookie ck = new HttpCookie("userinfo");
ck.Values["fname"] ="Amit";
ck.Values["lname"] ="Singh";
ck.Expires = DateTime.Now.AddSeconds(300);
Response.Cookies.Add(ck);
ck.Values.Remove("lname");


Example with Cookies ……………………………………………..

protected void btnGetCookies_Click(object sender, EventArgs e)
{
if ( Request.Cookies["count"]!= null)
{
i=int.Parse(Request.Cookies["count"].Value);

}
i++;
HttpCookie ck = new HttpCookie("count");
ck.Value = i.ToString();
Response.Cookies.Add(ck);
Response.Write(i.ToString());

}



What is the difference between persistent vs. non persistent cookies?

Persistent cookies are stored in a text file on the clients computer.
Non-Persistent cookies are stored in RAM on the client and are destroyed when the browser is closed.
Session cookies are created when you create a session object. Session can be created without cookies but that make the url look crappy.
Session is Non-Persistent...
You can make authentication cookie Persistent if you want.
Create Cookies on Default3.aspx …………………………………
protected void btnGetCookies_Click(object sender, EventArgs e)
{
// non persistant Cookies
Response.Cookies["txtno1"].Value = txtno1.Text;
// persistant Cookies
Response.Cookies["txtno2"].Value = txtno2.Text;
Response.Cookies["txtno2"].Expires.AddDays(14);
Response.Redirect("Default4.aspx");

}
How to get Cookies values to another page Default4.aspx …………………………………
protected void btnGetCookies_Click(object sender, EventArgs e)
{
txtno1.Text = Request.Cookies["txtno1"].Value;
txtno2.Text = Request.Cookies["txtno2"].Value;
}

4 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training in velachery | dot net training in chennai

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice site....Please refer this site also Our vision success!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Real time methodologies
    Asp.Net Training in Chennai | Dot Net Training Center in Chennai
    Software Testing Institutes in Chennai | Manual Testing Training in Chennai
    Java Training Institute in Chennai | Core Java Training in Chennai
    PHP training in chennai | Best PHP Training in chennai


    ReplyDelete