session

Posted by Pallavistv under ASP.NET on 4/14/2009 | Views : 1170 | Status : [Member] | Replies : 3
how we use session and i want full coding and informaiton to use it




Responses

Posted by: Vuyiswamb on: 4/14/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Afternoon Pallavistv

Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead. Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file. Markup code:

<%@ 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>Sessions</title>
</head>
<body runat="server" id="BodyTag">
<form id="form1" runat="server">
<asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged">
<asp:ListItem value="White" selected="True">Select color...</asp:ListItem>
<asp:ListItem value="Red">Red</asp:ListItem>
<asp:ListItem value="Green">Green</asp:ListItem>
<asp:ListItem value="Blue">Blue</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>


And here is the CodeBehind:

using System;

using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["BackgroundColor"] != null)
{
ColorSelector.SelectedValue = Session["BackgroundColor"].ToString();
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
}
}

protected void ColorSelector_IndexChanged(object sender, EventArgs e)
{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
Session["BackgroundColor"] = ColorSelector.SelectedValue;
}
}


As you can see, the example doesn't need a lot of changes to use sessions instead of cookies. Please notice that session values are tied to an instance of your browser. If you close down the browser, the saved value(s) will usually be "lost". Also, if the webserver recycles the aspnet_wp.exe process, sessions are lost, since they are saved in memory as well. This can be avoided by saving session states on a separate StateServer or by saving to a SQL server.

Thank you for Posting at Dotnetfunda

Were are looking forward for your reply

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Pallavistv, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 4/14/2009 [Member] Bronze

Up
0
Down
You can get the details related to session from the following links
http://www.w3schools.com/ASP/asp_sessions.asp
http://www.aspfree.com/c/a/ASP.NET/Application-and-Session-Objects-in-ASP.NET/

Pallavistv, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kunal Pawar on: 4/15/2009 [Member] Starter

Up
0
Down
Hi,
In Asp.net there are some server variable. Which are Application and Session.Application variavle used to store information for whole application. And sessions are used to store information for client. In global.asax file session related events are used. Session is live still client ias live. If client browser is closed then session automatically get closed.

Create/ set session:-
VB.Net
Session("Sessino Name") = value
C#
Session["Sessino Name"] = value;

To get value from Session
VB.Net
variable = Session("Sessino Name")
C#
variable = Session["Sessino Name"];



Pallavistv, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response