Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4060750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:22:42+00:00 2026-05-20T15:22:42+00:00

At times when we access Session[key].ToString() it gives exception when session is expired &

  • 0

At times when we access Session["key"].ToString() it gives exception when session is expired & in coding we try to access session variable. So I’m trying to create extension method on object class so that I could write it as Session["key"].getString() in coding so that every time I don’t have to do Convert.ToString(session["key"])

Any other solutions are also appreciable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:22:43+00:00Added an answer on May 20, 2026 at 3:22 pm

    Just use the null-coalescing operator:

    string value = (session["key"] ?? String.Empty).ToString();
    

    Update
    If you must have a method to do this (extension or otherwise), I would do something like:

    public static string GetValue(this HttpSessionState session, string key)
    {
       // TODO: Insert appropriate error checking here.
    
       return (session[key] ?? String.Empty).ToString();
    }
    

    I might even go so far as to make it generic for other possible types with a GetValue call that takes a selector and then use lambdas:

    public static T GetValue<T>(this HttpSessionState session, string key, Func<object, T> valueSelector)
    {
        return valueSelector(session[key]);
    }
    
    public static string GetStringValue(this HttpSessionState session, string key)
    {
        return session.GetValue(key, x => (x ?? String.Empty).ToString());
    }
    

    You would then use as follows:

    string value = session.GetStringValue("key");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to reduce access times to ClearCase a bit, and am thinking about
Every time I try to access my localhost/phpmyadmin it gives me this error --->
I need to access the value of a bound item several times in a
Lots of times when I use an application that needs perforce access (Visual Studio,
I'm running into a scenario where at random times I am getting Access Denied
I'm trying to add <session-management> in my Spring Security namespace configuration so that I
I'm unable to access the session_key in my custom Django middleware. I try to
I have had several occasions recently to access a specific class several times over
Anyone have any idea of how to create a infinite session so the user
I am getting Nhibernate Session closed exception with asp.net mvc telerik ajax grid. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.