Problems with cookies:
1.cookie can store only string type data [storing array, dataset object.. is not possible]
2.cookie can store maximum of 4kb data
3.cookie doesn’t provide security
4.browser is having capability of disabling cookies, in this case website using cookies will not function properly
Tool menu->internet options
Select privacy tab
Click on advanced button
First part cookies
Accept block
5.selecting block option will inform browser not to accept cookies
The solution is session concept
Session:
->Website allocating block of memory unique to client with in server system is called “session”.
->session can be used to maintain user information across different webpages with in website by storing with in server system
->session will be maintained with the default timeout 20 mins [timeout will be considered from last access time]
Storing data[variable] into session:
Session[“varname”]=value;
->the value will be stored in the form of an object, It supports storing any type of data
->there is no restriction for memory size it can be any amount of data
Reading session variable:
Session[“varname”]->it returns value in the form of an object, It should be type casted to required type
Ex:
Session[“varname”].Tostring() (datatype) session[“varname”]
Creating website to work with session concepts
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\sessionsite[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 default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-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 design part
username
password
next |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string[] s = new string[6];
s[0] = TextBox1.Text;
s[1] = TextBox2.Text;
Session["user info"] = s;
Server.Transfer("default2.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 default2.aspx
Goto design part
Addr
Emaild
next |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string[] s = (string[])Session["userinfo"];
s[2] = TextBox1.Text;
s[3] = TextBox2.Text;
Session["userinfo"] = s;
Server.Transfer("default3.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 default3.aspx
Goto design part
Gender male
female
mobile
register |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string gender = "";
if (RadioButton1.Checked)
gender = "male";
else
gender = "female";
string[] s = (string[])Session["userinfo"];
s[4] = gender;
s[5] = TextBox1.Text;
Session["user info"] = s;
Server.Transfer("register.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 register.aspx
Goto design part
Place label control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] s = (string[])Session["userinfo"];
string info = "name:" + s[0] + "<br>";
info = info + "password:" + s[1] + "<br>";
info = info + "addr:" + s[2] + "<br>";
info = info + "emailid:" + s[3] + "<br>";
info = info + "gender:" + s[4] + "<br>";
info = info + "mobile:" + s[5];
Label1.Text = "userinfo <br>" + info;
}
}
set default.aspx as start page
[right click on default.aspx and set as start page]
Goto contrl F5