3
Reply

.NET/ASP.NET Interview Question -Why do we need Sessions?

    Simply Session helps you to create a named container of data which can be used through the application till the user logged in.

    Hai,

    Session is used to store the value of the variables and possible to access it throughout a session.Incookies,You can pass the variables value thro response

    object and you can get that thro request object.But cookies are all upto storing users data in browser nly(i.e client side).But Sessions are used to hold data

    throughout a session in server side memory(Server Side).You can expire the session by using SessionTime out Property.We can use sessions in authentication

    and authorization process for validation also.

    To store Session Values,

    Session["UserID"]=txt_UserID.Text.ToString();

    To retreive Session Values,

    if(Session["UserID"]!=null)

    {

    lbl_welcomelbl.text="Welcome"+Session["UserID"];

    }

    For Article,

    http://www.codeproject.com/KB/aspnet/ExploringSession.aspx

     

     

    Answer:

    HTTP is a stateless protocol; it can't hold the client information on page. In other words after every request and response the server does not remember the state, data and who the user was. If user inserts some information, and move to the next page, that data will be lost and user would not able to retrieve the information.So, Session provides that facility to store information on server memory.

    Below is the diagram to understand in better manner.




    In the above example, when the user request the IIS server for the Page1.aspx then the request is broadcasted to the user/client browser and the connection is broken, Now when the same user request the IIS server for the Page2.aspx then again the request is broadcasted to the user/client browser but this time again the same user is treated as a new user as the connection was broken by IIS server after displaying the Page1.aspx page.

    Note:-
    So every single time a new request is made the same user is treated as a new one, so in order to maintain this we need Sessions.

    Regards,

    Please click here to see more .NET/ASP.NET interview questions