C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Use a session variable in the class files in c# ASP.Net


Posted Date: 20 Dec 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Miss Meetu ChoudharyMember Level: Diamond    
Rating: 1 out of 5Points: 5



To use the value of a session variable in the class files using c# with ASP.Net use the following statement

direct use of Session collection is not allowed in the .cs files so to achieve the purpose we have to follow the whole path i.e

System.Web.HttpContext.Current.Session["varname"].ToString()

Example:

System.Web.HttpContext.Current.Session["con1"].ToString()

++
Thanks and Regards
Meetu Choudhary



Responses

Author: Anand Babu S    23 Dec 2008Member Level: Gold   Points : 2
Imports System.Web.SessionState

'''
''' Encapsulates the details of managing session variables.
'''

Friend NotInheritable Class MySession

Private Const UserIdKey As String = "UserId"

Private Sub New()
End Sub

'''
''' Returns the current Session object, if any.
'''

Private Shared ReadOnly Property Session As HttpSessionState
Get
Return HttpContext.Current.Session
End Get
End Property

'''
''' Returns True if a valid Session object exists.
'''

Public Shared ReadOnly Property Exists() As Boolean
Get
Return Not Session Is Nothing
End Get
End Property

'''
''' Gets the ID of the user who is currently logged onto
''' the system.
'''

Public Shared Property UserId As Integer
Get
Return DirectCast(Session(UserIdKey), Integer)
End Get
Set(ByVal value As Integer)
Session(UserIdKey) = value
End Set
End Property

End Class



Author: Prashant Mishra    26 Dec 2008Member Level: Bronze   Points : 0
Hi meetu

Is there any way to use Request and response in Class file??


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Use of session variable in cs file  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Exception Handling
Previous Resource: Sample program using feilds,constructors,properties
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use