Home > ASP.NET > How to pass list of items using session in asp.net



How to pass list of items using session in asp.net

In this article we have described how to pass list of items using session in asp.net


In this article we have described how to pass list of items using session in asp.net.

we have provided example with an sample code .Session management an way to pass data from one page to another page.

session management store values that are 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)

    {

        string connection =                                   Convert.ToString(ConfigurationManager.ConnectionStrings["Connection"]);

        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["UserInfo"] = GetUserInfo(idr);

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

        }

        else

        {

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

        }

  }

 

    public userinfo GetUserInfo(SqlDataReader idr)

    {

        return new userinfo

        {

            userName = Convert.ToString(idr["Username"]),

            userId = Convert.ToInt32(idr["UserId"])

        };

    }

 

View-Info C# page

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

public string username = string.Empty;

protected void Page_Load(object sender, EventArgs e)

 {

  userinfo userData = (userinfo)Session["UserInfo"];

  username = ((userinfo)Session["UserInfo"]).userName;

 }

 

View-Info aspx page

We have used the string value below on aspx page.You can have a look at it and learn.

<body>

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

        <div>

            Hi <% =username %>

        </div>

    </form>

</body>

 

Download



Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*