You must Sign In to post a response.
  • Category: .NET

    How to use session in Asp.net

    hello sir,
    i want to know that how to use session.
  • #723578
    Hi,

    Check these links

    http://www.dotnetspider.com/resources/44968-ASP.net-Statemanagement.aspx

    http://www.dotnetspider.com/resources/25388-Sessions-asp-net-A-look.aspx

    http://www.dotnetspider.com/resources/1878-State-Management-ASP-Net.aspx

    http://www.dotnetspider.com/tutorials/AspNet-Tutorial-29.aspx

    Thanks & Regards,
    Mahesh Durgam

  • #723583
    Hi,

    Session is one the techniques to use data from one webform to another others include querystrings,cookies etc.

    Session state variables are stored on the webserver by default and are there for lifetime of a session.

    If you want to store any value eg a text box value in a session variable

    Session["name"]=txtname.text;

    Here "name" is the key.

    If you want to retrieve value from session variable after the webform is redirected to another web form

    LblName.text=Session["name"].ToString();

    The lifetime of session is determined by timeout value in web.config file.You can adjust it according to your requirement.

    <Sessionstate mode="Inproc" timeout="20">

    Aparna

  • #723597
    Hai,

    Using session we can maintain state for multiple browsers.

    Session is started Automatically by ASP.net for every new user. Every session is provided with session Id which is internally a COOKIE created by ASP.net. Session Id will travel between every request & response. A session is enabled by ASP.net based on idle timeout. By default session idle timeout is 20min. which means if user doesn't request for server for 20min, then server will destroy the session. we can change this time in web.config as well as programatically using session object.

    When Application starts…..?

    Application started automatically when first user make a request

    When Session starts….?

    When new user make a request session will starts.

    When Pool starts…?

    When IIS will starts pool started.

    When pool ends…?

    When IIS will end pool ended.

    When Session ends….?

    Idel time 20 mins session will end.

    When Application ends…?

    All sessions are ended then Application end.


    EX:


    Protected void btnSubmit_Click (Object sender,EventArgs e)
    { Session["User"]=txtUser.Text; Response.Redirect("online.aspx");
    }




    Protected void Page_Load(object sender,EventArgs e){
    If(Session["User"]!=null) Lbl.Text="Hi ,"+Session["User"].ToString();
    Else
    Lbl.Text="Guest";
    }


    for more information you can search in Google you got no.of results out of this..

    hope this information is helpful to you..

    Mark this post if it helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #723661
    Hi,

    Please check below example,


    protected void btnShowFile_Click(object sender, EventArgs e)

    {
    Session["FileID"] = "1";
    Response.Redirect("ShowFile.aspx");
    }


    In your ShowFile.aspx page


    protected void Page_Load(object sender,EventArgs e)
    {
    if (!IsPostBack)
    {
    if (Session["FileID"]!=null && Session["FileID"].ToString() == "1")
    {
    //Code to display file
    }
    }
    }



    Let me know if you have any doubt in this approach.


    Regards,
    Asheej T K

  • #723664
    Hi,

    Session is used to maintain values across pages. You can assign values in any page and get back in the any page.


    //Initialize
    session["loginuser"] = "name of the user";

    //Calling like this
    Label1.Text = Session["loginuser"].ToString();

    //Remove like this
    Session.Remove("loginuser");

    //Remove all session values
    Session.RemoveAll();



    For more details refer my article about state management technique
    http://www.dotnetspider.com/resources/44056-What-ASP.NET-State-Management.aspx

    Regards
    N.Ravindran
    Your Hard work never fails

  • #723682
    HI,

    You can use session to store the login details which you can use it for the other pages.

    To set the session. Try the following syntax.

    Session["loginname"]= "deva";


    to fetch the session value. You can try the following syntax


    string name = Session["loginname"].ToString();


    Hope it will be useful.

    Regards,
    T.N. THEAAVARAAJ
    Senior Software Engineer,
    Microsoft Certified Technology Specialist.
    Email:devabe2005@gmail.com

  • #723684
    [Response removed by Admin. Read forum policies.]

  • #723793
    Session Object:
    It saves the data for all pages for single user. It holds the data as object type. So while reading session we should cast it. session variables are used to store the User based unique values..and you can set the time limit to store the values in the session state variables..
    A session starts when:
    A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure
    A value is stored in a Session variable
    A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope.
    Session["StockPicks"] = stockPicks;
    // When retrieving an object from session state, cast it to
    // the appropriate type.
    ArrayList stockPicks = (ArrayList)Session["StockPicks"];


    To set session value.
    Session["UserName"] = txtUserName.text;


    To retrieve session value.
    if (Session["UserName"] != null)
    {
    string userName = Session["UserName"] as string;
    }

  • #734763
    Session is used to store the data.Session automatically expires in 20 minutes. if you want we can increase or decrease the time limit.Here i gave how to get session value in javascript.


    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class _Default : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    Session["Tes"] = "Test";
    }
    }
    }


    <%@ 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>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function x()
    {
    var sessionValue = '<%= Session["Tes"] %>';
    document.getElementById("TextBox1").value=sessionValue;
    return true;
    }
    </script>

    </head>

    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" OnClientClick="return x()" runat="server" Text="Button" />
    </div>
    </form>
    </body>
    </html>



    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.