SESSION

Whenever client sends request to server for the first time server will process the client request ,allot some memory .To this client known as Session and a unique Id is given known as SessionId.Then Every time response from the server ,request form the client will be transferred using this Id.



S1:-client sends the request to the server for the first time.
S2:-server will process the client request, allot memory to the client known as Session. unique id is given i.e.,SessionId
S3:-Response is delivered to the client along with SessionId.
S4:-client will send the next request along with Session Id.
  • As long as client working with the server the request & response will be transferred using the same session Id.
  • When session is destroyed at Server Session Id will also Destroyed.
  • A Session is destroyed in two situations
  • When session timeout period completes
  • When we kill the session programmatically using Session.Abandon().
  • Default timeout period of a session is “20” minutes.
  • In general a session is local to particular client.
  • The session data of any session is accessible to particular client by whom it is created in general any client cannot access other clients session data.
  • A session Id is “120 bits/15 bytes” key maximum sessions are created at server are 2120
  • Within this session we can create variables called session variable

 To create Session Variables we use the Syntax like:-


              Sy:-   Session[“Variable Name”]=Data;
              Ex:-   Session[“a”]=20;
                        Session[“b”]=20.5
                        Session[“UName”]=”sampath"


To access Session variables we use Index value or variable name like:-
   
Session[0]
   OR
Session[“a”]

Session[1]
     OR
Session[“b”]

Session[2]
     OR
Session[“UName”]

  • Session variables will store the data & Return the data with object type .
  • Session can store any amount of data in general there is no restriction for the memory of the session
  • For each browser request sent from a single machine a separate session will be created at Server side.
Methods with Session Object:-
[1] Abandon()  : - used to kill the Session programmatically
Properties with Session :- 
[1] SessionId : -This is Readonly property which returns the session Id of current Session“Session Id is 120 bit code” automatically generated by server.                

Example to work WithSession:-

Create Two WebPages SourceS.aspx,TargetS.aspx

Design SourceS.aspx by taking a Textbox & a Button like



 
Set the following Properties for theTextBox1
    
ID                    ---        txtEmpId

Set the following Properties for the Button1
 
 ID                    ---        btnSubmit

Name               ---        Submit

Goto Source and write the Following Code in btnSubmit Click Event:-

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Session["EmpId"] = txtEmpId.Text;
        Response.Redirect("TargetSs.aspx");
    }

             

Design TargetS.aspx by taking a GridView like
 
Goto Source and write the following code in Page Load Event of TargetS.aspx
           
public partial class TargetSs : System.Web.UI.Page
{
SqlConnection Con = new SqlConnection("Server=.;User Id=sa;password=Admin123;DataBase=Employee");
          SqlDataAdapter Da;
         protected void Page_Load(object sender, EventArgs e)
          {
string q = "Select * from EmpDetails where EmpId=" + Session["EmpId"].ToString();
            Da = new SqlDataAdapter(q, Con);
            DataSet Ds = new DataSet();
           Da.Fill(Ds);
           GridView1.DataSource = Ds.Tables[0];
           GridView1.DataBind();
          }
}





No comments:

Post a Comment