473,569 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Http Context.Current .Session["abc"].ToString();

Thanks

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

You have to use an Object :

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

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

"Tony" <To**@discussio ns.microsoft.co m> a écrit dans le message de
news:06******** *************** ***********@mic rosoft.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.Http Context.Current .Session["abc"].ToString();

Thanks

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

"Tony" <To**@discussio ns.microsoft.co m> wrote in message
news:06******** *************** ***********@mic rosoft.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.Http Context.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.Http Context.Current .Session["abc"] ;
string str = string.Empty ;

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

"Tony" <To**@discussio ns.microsoft.co m> a écrit dans le message de
news:06******** *************** ***********@mic rosoft.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.Http Context.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**@discussio ns.microsoft.co m> wrote in message
news:DA******** *************** ***********@mic rosoft.com...
Thanks - I new it would be an easy answer!!!
"Sacha" wrote:
Hello...

You have to use an Object :

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

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

"Tony" <To**@discussio ns.microsoft.co m> a écrit dans le message de
news:06******** *************** ***********@mic rosoft.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.Http Context.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
3171
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 unless your session ends One more thing Session variables can very much be used in Application events
3
3453
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 details. I have nav Link buttons that allow you to go to previous or next object from the object list on previous page. In the command event of the...
14
3207
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 go to is a login form, which will set several session variables with the name used to log in, appropriate security level and some other misc...
5
2440
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 variables and all of them are being stored into session and accessed from session and individual session object. Example: Session = "XYZ", Session=100,...
8
3162
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, if a user logs onto the website, a user object is created that stores their full name, email address, street address, phone, etc. This object also...
14
2249
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 at a page.I have a search criteria to search the records based on the data range i give This is what i have done, in the !IsPostBack section. I...
13
1891
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") + 1 Why? Thanks Dee.
3
1564
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. At the start of each session I initialise this TestObject. On entering every page I create a local TestObject and do this: TestObject =...
13
1741
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 properties, I have to store it back into the session object to "update" it, right? Or will the changes to my object automatically be saved back into the...
12
2198
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 thing because it forces IIS to affiliate the Session with the Thread on which the STA object is created. This means all subsequent requests for...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.