Home > ASP.NET > how to save and retrieve session value in asp.net



how to save and retrieve session value in asp.net

In this article we have described how to save and retrieve session value one page to another page in asp.net


In this article we have described how to save and retrieve session value one page to another page in asp.net

with an example and sample code. Session management a way to pass data from one page to another page.

session management store values that are the global application you can use single user and all users as per requirement. Session is a server side control basically is used to management the user login but so many purpose for use session management.

 

Required Namespaces:

you will need to use the following namespaces.

using System.Configuration;

using System.Data.SqlClient;

 

aspx page

We have used the session management code below. You can have a look at it and learn.

<html>

<head runat="server">

    <title>how to use session management in  asp.net </title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:TextBox ID="txtUserID" placeholder="User Id" class="form-control" runat="server" />

            <asp:TextBox ID="txtPassword" placeholder="Password" class="form-control" TextMode="Password" runat="server" />

            <br />

            <asp:Button ID="btnSignIn" Text="Sign in" type="submit" OnClick="OnValidateUser" class="btn-primary" runat="server" />

        </div>

        <asp:Label ID="lblerror" Text="" runat="server" />

    </form>

</body>

</html>

 

 

C# code

We have explained below C# code with an examples sample login and session used in this article .You can have a look at it and learn.

  protected void OnValidateUser(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection(connection);

        SqlCommand cmd = new SqlCommand(String.Format("Select * from Users where userName ='{0}' and password='{1}'", txtUserID.Text.Trim(), txtPassword.Text.Trim()), con);

        con.Open();

        SqlDataReader idr = cmd.ExecuteReader();

        userinfo user = new userinfo();

        if (idr.Read())

        {

 

            Session["UserId"] = idr["userId"];

            Session["UserName"] = idr["Username"];

            Response.Redirect("View-Info.aspx");

        }

        else

        {

            lblerror.Text = "Error : Please enter currect user name and password !";

        }

    }

 

    public class userinfo

    {

        public int userId { get; set; }

        public string userName { get; set; }

        public string password { get; set; }

        public string role { get; set; }

    }

 

aspx page

We have get the session value code below on aspx page.You can have a look at it and learn.

<body>

    <form id="form1" runat="server">

        <div>

            Hi <%=Session["UserName"%>

        </div>

    </form>

</body>

 

Download



Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*