How to implement Session in C sharp?

Jak
Jak
Member
364 Points
152 Posts

Hi,

How to implement Session in C sharp?

Views: 8245
Total Answered: 3
Total Marked As Answer: 2
Posted On: 09-Apr-2015 02:59

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Admin
404 Points
67 Posts
         

Hi Jak,

Use following method:

public static bool IsSessionExpired(string SessionText)
{

bool IsExpired = false;
if (HttpContext.Current.Session[SessionText] == null) {
IsExpired = true;
}
return IsExpired;

}

public static string GetSessionValue(string sessionname)
{

if (HttpContext.Current.Session[sessionname] == null) {
return "";
} else {
return HttpContext.Current.Session[sessionname];
}

}

public static bool SetSessionValue(string SessionName, string SessionValue)
{

try {
HttpContext.Current.Session.Add(SessionName, SessionValue);
return true;
} catch (Exception ex) {
return false;
}

}
Posted On: 08-Apr-2015 14:53
NiceOne Team
NiceOne...
Admin
404 Points
67 Posts
         

Hi Jak,

In VB.NET:

Public Shared Function IsSessionExpired(ByVal SessionText As String) As Boolean
Dim IsExpired As Boolean
If HttpContext.Current.Session(SessionText) Is Nothing Then
IsExpired = True
End If
Return IsExpired
End Function

Public Shared Function GetSessionValue(ByVal sessionname As String) As String
If HttpContext.Current.Session(sessionname) Is Nothing Then
Return ""
Else
Return HttpContext.Current.Session(sessionname)
End If
End Function

Public Shared Function SetSessionValue(ByVal SessionName As String, ByVal SessionValue As String) As Boolean
Try
HttpContext.Current.Session.Add(SessionName, SessionValue)
Return True
Catch ex As Exception
Return False
End Try
End Function
Posted On: 08-Apr-2015 15:01
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         

Hi Jak,

If you want to reload on back button of browser that's if you do not want to cache the the page in brwser use following code on page load: 

HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false); 
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

 

Posted On: 20-Jul-2015 05:13
 Log In to Chat