Pages

Search This Blog

Friday 9 May 2014

What is session?? What is use of it ??

Hi friends, today I will say you “how to create sessions in ASP.NET?”

Before saying that I just want to say “What is the use of sessions in our application/website?? Why we must use it??”

For a website they will be different users and the data they can access or view are different. Normally in application we create many pages and we will redirect from one page to another page when the user do specific task.

For example after “login” user wants to edit his profile. Then he presses the button “edit”. Here we must show the data of his profile only which was dynamic. To retrieve data from the database we must know which users data we must bring from database. At that time we need to know which user was login. So, at the time of login only we will store “Username” in session and we can access anywhere in our application.

In such cases we use “Sessions” and “Cookies”.
// Storing textbox value in Session named “username”

Session["username"] = txtusername.Text;

// Retrieving session value where ever we need and I am storing in a //string named as username

string username = Session["username"].ToString();

Example:

LOGIN.ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>How to use Session’s</title>
</head>

<body>

    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
   
            <tr>
                <td align="right">
                    User Name
                </td>
                
                 <td>
<asp:TextBox ID="txtusername"runat="server" Width="180px"></asp:TextBox>
                </td>
            </tr>
            
           <tr>
                <td align="right">
                    Password
                </td>
                <td>
                    <asp:TextBox ID="txtpwd"runat="server" Width="180px" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            
            <tr>
                <td align="right">
                </td>
                <td>
                            <asp:Button ID="btnsignin"runat="server" onclick="btnsignin_Click"
                        Text="Sign in" Width="180px" />
                </td>
            </tr>

       </table>

        </fieldset>

    </div>
    </form>

</body>

</html>

LOGIN.ASPX.CS:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class sessionvalue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
 protected void btnsignin_Click(object sender,EventArgs e)
    {
        
    Session["username"] = txtusername.Text;
       
     }
   
}



  •  We can pass the value while redirecting the page also as follow

 Session["username"] = txtusername.Text;

 string username = Session["username"].ToString();
           

 Response.Redirect("main.aspx?username=" + username);


  • To end/kill the session we will use the below code
        // It removes all session values
        
        Session.RemoveAll();

        Session.Abandon();
  • To set session timeout
In your web.config file and write the below code :


configuration>
  <system.web>
     <sessionState timeout="20"></sessionState>
  </system.web>
</configuration>


 You can rate my classes on right side top of my blog..... 



                                                                           -Your's santoshklpkl

No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget