473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Session object question!

how do I check if the data in the session object is null before extracting it.
The following throws an error if the Session object has not already been
created.

string str = System.Web.HttpContext.Current.Session["abc"].ToString();

Thanks

Nov 18 '05 #1
4 1305
Hello...

You have to use an Object :

object o = System.Web.HttpContext.Current.Session["abc"] ;
string str = string.Empty ;

if (o != null)
str = o.ToString() ;

"Tony" <To**@discussions.microsoft.com> a écrit dans le message de
news:06**********************************@microsof t.com...
how do I check if the data in the session object is null before extracting it. The following throws an error if the Session object has not already been
created.

string str = System.Web.HttpContext.Current.Session["abc"].ToString();

Thanks

Nov 18 '05 #2
if (Session["abc"] == null)
{
// abc is null
}
else
{
// Use abc
}

"Tony" <To**@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
how do I check if the data in the session object is null before extracting
it.
The following throws an error if the Session object has not already been
created.

string str = System.Web.HttpContext.Current.Session["abc"].ToString();

Thanks


Nov 18 '05 #3
Thanks - I new it would be an easy answer!!!
"Sacha" wrote:
Hello...

You have to use an Object :

object o = System.Web.HttpContext.Current.Session["abc"] ;
string str = string.Empty ;

if (o != null)
str = o.ToString() ;

"Tony" <To**@discussions.microsoft.com> a écrit dans le message de
news:06**********************************@microsof t.com...
how do I check if the data in the session object is null before extracting

it.
The following throws an error if the Session object has not already been
created.

string str = System.Web.HttpContext.Current.Session["abc"].ToString();

Thanks


Nov 18 '05 #4
> Thanks - I new it would be an easy answer!!!

Unfortunately, that was a klugy answer. You don't have to create an object
variable. All you need to do is test if the value is null before attempting
to assign it to a typed variable. Example:

string str; // declare your string
variable.
if (Session["abc"] != null) // Check if Session reference is null
str = (string) Session["abc"]; // Cast the Session object as a string
(or whatever it is) and assign it

This is much more efficient code. Also, it will work with any data type
stored in Session. Just change the cast.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
"Tony" <To**@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
Thanks - I new it would be an easy answer!!!
"Sacha" wrote:
Hello...

You have to use an Object :

object o = System.Web.HttpContext.Current.Session["abc"] ;
string str = string.Empty ;

if (o != null)
str = o.ToString() ;

"Tony" <To**@discussions.microsoft.com> a écrit dans le message de
news:06**********************************@microsof t.com...
how do I check if the data in the session object is null before extracting
it.
The following throws an error if the Session object has not already

been created.

string str = System.Web.HttpContext.Current.Session["abc"].ToString();

Thanks


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available...
3
by: | last post by:
This is a very open ended question. I have an app with a page that uses link buttons to change the display. On the prior page you have a list of objects. You click one of those objects to see it's...
14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
5
by: Abhilash.k.m | last post by:
This is regarding the session management using Out of proc session management(SQL SERVER). Among the samples below which one is better to set the session? 1. There are 20 session...
8
by: Dave Wurtz | last post by:
All, I'm new to ASP development and I have a basic design question: Is it ok to store business objects to session variables or is there a better way to keep object information? For example,...
14
by: Venkat Chellam | last post by:
I have a peculiar problem. I have a simple web application which loads some data from the oracle table and display in the datagrid in the webpage and datagrid has page enabled which shows 10 rows...
13
by: dee | last post by:
Hi My code complies the following line: Session("passed") = 1 but puts wiggly error line under the second Session("passed") in the following expression: Session("passed") = Session("passed") +...
3
by: MrShovel | last post by:
I'm new to this ASP.NET caper and have the following questions. I have a TestObject that contains about 50 fields of data and 3 member procedures. Below is a simplified explanation of what I do....
13
by: | last post by:
Simple question, I think... I'm storing an object in the Session object. In the code behind I read that object: trx = CType(Session("Transaction"), BOCSTransaction) If I change any...
12
by: Anthony Jones | last post by:
Here's a question that I've not been able to get a definitive answer to. Creating an STA object (such as a typical VB6 object) and assigning to the ASP Session store is a bad thing. It's a bad...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.