SESSION
Ø
THIS IS THE OBJECT TO MANAGE THE STATE AT SERVER SIDE
Ø
HERE WE
MAINTAINED THE USER SPECIFIC DETAILS ON SERVER
Ø
SESSION OBJECT LIKE INSTANCE DATA MEMBER
Ø
SESSION IS NOT A SINGLE OBJECT BASED FOR THE USER IT
PROVIDED DIFFERENT-2 OBJECT FOR EACH USER TO MANAGE THEIR UPDATE VALUE ON THEIR
SIDE ON THE BASIS OF THE SESSTION ID
Ø
SESSION ALWAYS USE SERVER RESOURCES
Ø
SESSION IS LIKE A PROPERY BUT IT IS REPRESENTING
HTTPSESSIOSNSTATE CLASS
Ø
SESSION PROVIDING AN MEMORY TO THE USER ACCORDING TO
THE SESSION ID OBJECT AND A USER CAN RETREIVE IT BY THE OBJECT
SESSION ID
SESSIO ID IS AN UNIQUE NUMBER WHICH IS
GENERATED ACOORDING TO THE SESSTION RANDOM NUMBER. IT IS 24 CHARACTER RANDOM NUMBER
WHICH CAN NOT BE DUPLICATE, IT IS CREATED ONCE IN A ONE TIME, IT CHANGE
EVERYTIME UNTILL A USER PASS A VALUE IN SESSION STATE
SESSION CREATE AN COOKIE AND KEEP THE SESSION ID IN TO IT AND SEND TO
THE CLIENT BROWSER SIDE WHEN A CLIENT SEND A REQUEST TO SERVER.
SESSION TABLE
SESSION TABLE IS CREATE BY THE SERVER TO
MANAGE MULTIPLE SESSION OBJECTS ON THE BASES OF SESSION ID WHICH IS KEEPING THE
VALUE OF EACH SESSION IF IN SESSTION OBJECT
SESSION ID
|
SESSION OBJECT
|
ASFASDFASFASFAFAS
|
101
|
SDGSDGSDG
|
102
|
SDFGSDG
|
103
|
ASP.NET_SESSION
ID
THIS IS A SESSION COOKIE IN
WHICH IS CREATED BY THE BROWSER TEMPORARY WHEN BROWSER OPEN PAGE AND DELETED
AUTOMATICALLY WHEN BROWSER IS CLOSED.
Ø
HERE TO MANAGE THE REQUEST OBJECT IN SESSION TABLE, WE
USE SESSION TIME OUT BY DEFAULT FOR ASP.NET APPLICATION IT IS 20 MINUTES, BUT WE CAN CHANGE IT ACCORDING TO REQUEST,
THIS TIMING IS NOT ABSOLUTE, IT IS SLIDING
Ø
IF ANY REQUEST DID NOT SENT BY THE
CLIENT TO SERVER FOR 20 MINUTES THAN THE SERVER DELETE THIS SESSTION OBJECT
FROM THE SESSTION TABLE
Ø
WE CAN ALSO
FORCEFULLY DELETE THIS SESSTION OBJECT BEFORE TIME
Session.Abandon();
Delete data and object both
Session.Remove("data");
TO DELETE A PRATICULAR KEY DATA
Session.Clear();
HERE THE DATA IS
REMOVED BUT SESSTION OBJECT IS REMAINED
·
IT ALSO FOLLOWS THE DICTIONARY STRUCTURE TO STORE THE
SESSTION ID
WORKING
STRUCTURE OF WHEN TWO CLIENT REQUEST ON SESSIO ID
CLIENT 1
|
SESSION ID
|
SESSION OBJECT
|
ASFASDF
|
101
|
SDGSDG
|
102
|
SDFGSDG
|
103
|
SAME FOR THE NEXT AND EVERY CLIENT
THERE ARE TWO
MODE OF SESSION ID TO STORE
INPROC MOD OUTPROC
MOD
INPROC MODE
DATA OF SESSTION IS
STORING IN PROCESS WHERE YOUR APPLICATION IS RUNNING AND OUR PROCESS IS RUN
UNDER IIS SERVER HERE WE MAINTAIN THE NONPRESISTENT COOKIE, AND THIS NON
PRESISTENT COOKIE IS MAINTAINT UNTILL THE CLIENT DID NOT CLOSE THE BROWSER
OUT PROC MODE
HERE
WE STORE THE DATA IN THE OUT OF THE PROCESS NOT IN APPLICATION PROCESS
THERE
IS TWO WAY TO STORE YOUR SESSION ID AT SERVER SIDE AND OUT OF THE PROCESS
STATE SERVER
SQL SERVER
GLOBAL.ASAX (GLOBAL APPLICATION FILE)
GLOBAL
APPLICATION IS AN FILE WHICH HAVE APPLICATION EVENT AND SESSTION EVENT AND
OTHER EVENTS ALSO
TO ADD IT ADD NEW
ITEMS GLOBALAPPLICATIONFILE OK
SESSION
START
SESSION START EVENT IS
FIRED YOUR APPLICATION IS STARTED AND IT IS NOT FIXED AND IT CHANGES EVERY
REQUEST WHEN YOU ADD SOME DATA IN SESSION THAN IT FIXED THE SESSION ID FOR THE
USER
SESSION END
THIS EVENT
IS FIRED WHEN YOUR SESSION OBJECT IS DELETED AND YOUR APPLICATION HAS CLOSED
protected void
Button1_Click(object sender, EventArgs e)
{
Response.Write(Session.SessionID);
}
protected void
set_Click(object sender, EventArgs e)
{
Session[TextBox1.Text] = TextBox2.Text;
}
protected void
get_Click(object sender, EventArgs e)
{
TextBox2.Text = Session[TextBox1.Text].ToString();
}
TO PRINT COOKIE DETAILS
protected void
Page_Load(object sender, EventArgs e)
{
//Response.Write(Request.Cookies[0].Name);
Response.Write(Request.Cookies[0].Value.ToString());
}
TO PRINT SESSION VALUE
protected void
Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = Session[TextBox1.Text].ToString();
}
TO STORE THE DATA IN A LIST
USING SESSION
FIRST ADD NAMESPACE
USING SYSTEM.COLLECTION.GENERIC;
List<string>
li = new List<string>();
protected void
ADDINLIST_Click(object sender, EventArgs e)
{
if(Session["data"]!=null)
{
li=(List<string>)Session["data"];
}
li.Add(TextBox3.Text);
Session["data"]=li;
}
TO SHOW DATA IN A LISTBOX
FROM SESSION
protected void inlist_Click(object
sender, EventArgs e)
{
List<string> li=(List<string>)Session["data"];
foreach(string s in li)
{
ListBox1.Items.Add(s);
}
}
ON A SECOND PAGE TO SHOW DATA
IN A LIST BOX FROM SESSION ID
List<string>
li = new List<string>();
protected void
Button2_Click(object sender, EventArgs e)
{
List<string>
li = (List<string>)Session["data"];
foreach (string s in li)
{
ListBox1.Items.Add(s);
}
}
Note:
Practical using a class
properties to add use details on a list and store it in session and acess it
any page with the help of session object
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)
{
}
List<student> li = new
List<student>();
protected
void Insert_Click(object
sender, EventArgs e)
{
if (Session["data"]
!= null)
{
li = (List<student>)Session["data"];
}
student ob = new student();
ob.Sid = Convert.ToInt32(TextBox1.Text);
ob.Sname =
TextBox2.Text;
ob.City =
TextBox3.Text;
li.Add(ob);
Session["data"] = li;
}
int
x;
protected
void First_Click(object
sender, EventArgs e)
{
List<student> li = (List<student>)Session["data"];
student s = li[x];
TextBox1.Text =
s.Sid.ToString();
TextBox2.Text = s.Sname.ToString();
TextBox3.Text =
s.City.ToString();
Session["count"] = x;
}
protected
void next_Click(object
sender, EventArgs e)
{
if (Session["count"]
!= null)
{
x = (int)Session["count"];
}
List<student> li = (List<student>)Session["data"];
student s = li[x];
TextBox1.Text =
s.Sid.ToString();
TextBox2.Text =
s.Sname.ToString();
TextBox3.Text =
s.City.ToString();
x++;
Session["count"] = x;
}
protected
void previous_Click(object
sender, EventArgs e)
{
if (Session["count"]
!= null)
{
x = (int)Session["count"];
}
x--;
List<student> li = (List<student>)Session["data"];
student s = li[x];
TextBox1.Text =
s.Sid.ToString();
TextBox2.Text =
s.Sname.ToString();
TextBox3.Text =
s.City.ToString();
Session["count"] = x;
}
protected
void last_Click(object
sender, EventArgs e)
{
if (Session["count"]
!= null)
{
x = (int)Session["count"];
}
List<student> li = (List<student>)Session["data"];
x = li.Count - 1;
student s = li[x];
TextBox1.Text = s.Sid.ToString();
TextBox2.Text =
s.Sname.ToString();
TextBox3.Text =
s.City.ToString();
Session["count"] = x;
}
}
TO SET THE TIMEOUT OF THE SESSION
OPEN
WEBCONFIG FILE
<system.web>
<!--
Set compilation
debug="true" to insert debugging
symbols into the
compiled page. Because this
affects performance, set
this value to true only
during development.
-->
<sessionState timeout="1" cookieName="abc"></sessionState>
TO SET THE SESSION ID BEFORE IN GLOBAL APPLICATION FILE THAN ADD VALUE
IN SESSION
ADD GLOBAL
APPLICATION CLASS
ADD NEW ITEM CLICK ON GLOBAL APPLICATION
CLASS
void Session_Start(object sender, EventArgs e)
{
Session.Add("abc", "koi values nahi he");
}
ON PAGE 1
TO INSERT DATA
protected void Button2_Click(object
sender, EventArgs e)
{
Session["abc"] =
TextBox1.Text;
Response.Redirect("Default2.aspx");
}
TO PRINT THE SESSION DETAILS
AND COOKIE DETAILS AND INFORMATION ON SECOND PAGE
protected void
Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Cookies.Count.ToString() + "<br/>");
//
TO COUNT THE NO. OF COOKIE CREATED
Response.Write(Request.Cookies[0].Name.ToString() + "<br/>");
//
TO PRINT THE COOKIE NAME
Response.Write(Session["abc"].ToString()
+ "<br/>");
//
TO PRINT SESSION VALUE
Response.Write(Session.IsNewSession.ToString() + "<br/>");
//
TO PRINT THAT IS SESSION NEW OR NOT
IT
RETURN BOOLEAN VALUE( TRUE AND FALSE)
1. IF SESSION IS ALLREADY CREATED MEANS THIS PAGE
IS COMING FROM ANY PREVIOUS PAGE TO WHOM SESSION IS ALLREADY CREATED THAN IT RETURNS FALSE
2. IF IT CREATE A NEW SESSION WHEN A CLIENT SEND A
NEW REQUEST THEN IT RETURN TRUE
Response.Write(Session.CookieMode.ToString()+ "<br/>");
// TO
PRINT THE COOKIE MODE
IF
YOU WANT TO SEE THAT YOUR SESSION IS MANAGED IN COOKIE OR NOT THAN
Response.Write(Session.IsCookieless.ToString() + "<br/>");
TO IMPLEMENT COOKIE LESS SESSION
OPEN WEB.CONFIG
<SYSTEM.WEB>
<SESSION STATE COOKIELESS=”AUTO DETECT”
“DEVICE
PROFILE
“TRUE”
“FALSE”
“USE
URL”
“USE
COOKIE”
AUTO DETECT
IF WE USE THIS MODE
AUTODETECT THAN SEVER IS NOT DEPENDENT ON CLIENT SECURITY. IT DOES NOT
DEPENDENT THAT CLIENT HAS ENABLE TO ACCEPT THE COOKIE OR DISBALE MEANS BLOCK TO
ACCEPT COOKIES FROM HIS SIDE, IT ALWAYS STORED
THE SESSION ID IN ITS OWN COOKIE AT THE BROWSER
TRUE
AND USEURI
IN THIS MODE YOUR SESSION ID IS STORED IN YOUR BROWSER URL ADDRESS BAR
WITH YOUR WEBSITE URL. IT IS ALSO NOT DEPENDENT ON CLIENT
USE
COOKIE OR FALSE
IN THIS MODE SERVER USED IT’S OWN COOKIE
TO STORE THE SESSION ID , IN THIS MODE SERVER ALSO NOT DEPENDENT ON CLIENT
BROWSER
OUT PROC MODE OF SESSION
STATE
SERVER SQLSERVER
BEFORE WE HAVE TO SELECT OUR
SESSION STATE MODE TO MANAGE AND TO STORE OUR SESSION ID IN SQL SERVER OR IN
STATE SERVER
OPEN WEB.CONFIG
<system.web>
<sessionState mode="StateServer"
“ OFF”
“SQL
SERVER”
OFF
IF WE DO NOT WANT TO MANAGE SESSION ID
SQLSERVER
IF WE WANT TO MANAGE OUR SESSION ID AT SERVER
SIDE STORE IT IN DATABASE
STATE
SERVER
IF WE WANT TO
MANAGE OUR SESSION ID IN STATE SERVER. STATE SERVER IS AN SERVICE WHICH ACT AS
AN SERVER ON CLIENT MACHINE TO MANGE THE SESSION ID
STATE SERVER
OPEN WEB.CONFIG
(FIXED PORT NO.)
<system.web>
<sessionState
mode="StateServer" stateConnectionString="tcpip=localhost:42424"></sessionState>
BUT TO WORK ON IT
FIRSTLY WE HAVE TO START THIS ASPSTATE SERVICE
OPEN CONTROL PANNEL ADMINISTRATIVE TOOL SERVICES
ASP.NET STATE
SERVICE RIGHT CLICK START
TO
SET STATE NETWORK TIME OUT
<system.web>
<sessionState
mode="StateServer" stateConnectionString="tcpip=localhost:42424" stateNetworkTimeout="5">
</sessionState>
SQL SERVER
TO MANAGE THE SESSION
ID AT SERVER SIDE
AT SERVER SIDE WE HAVE THREE
DATABASE OPTION TO STORE THE SESSION ID
WITH THE HELP OF DOS
COMMAND PROMPT
TEMPDB IT STORE THE INFORMATION TEMPORARY
Open visual studio command prompt
C:\\ program files\microsoft
visual studio \ vc> aspnet_regsql –ssadd –S . –E –sstype t
// if you
are using window authentication
C:\\ program files\microsoft visual
studio \ vc> aspnet_regsql –ssadd –S . –U sa –P 123 –sstype t
To remove
C:\\ program
files\microsoft visual studio \ vc> aspnet_regsql –ssremove –S . –U sa –P
123 –sstype t
NOW IN WEB.CONFIG
<system.web>
<sessionState mode="SQLServer" sqlConnectionString="Datasource=.;Itegrated Security=true"></sessionState>
FOR ASPSTATE
C:\\
program files\microsoft visual studio \ vc> aspnet_regsql –ssadd –S . –U sa
–P 123 –sstype P
To remove
C:\\ program files\microsoft visual
studio \ vc> aspnet_regsql –ssremove –S . –U sa –P 123 –sstype p
NOW
IN WEB.CONFIG
<system.web>
<sessionState mode="SQLServer" sqlConnectionString="Datasource=.;Itegrated Security=true"></sessionState>
FOR CUSTOME DATABASE(SESSION IS STORE IN
YOUR OWN DATABASE
C:\\
program files\microsoft visual studio \ vc> aspnet_regsql –ssadd –S . –U sa
–P 123 –sstype C –d sunil
To remove
C:\\
program files\microsoft visual studio \ vc> aspnet_regsql –ssremove –S . –U
sa –P 123 –sstype C –d sunil
NOW
IN WEB.CONFIG
<system.web>
<sessionState mode="SQLServer" allowCustomSqlDatabase="true" sqlConnectionString="Data Source=.; Integrated Security=true; Database=arun"></sessionState>
No comments:
Post a Comment