Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 04, 2012 01:14 PM by chalakiblog
Member
631 Points
629 Posts
Mar 02, 2012 04:46 AM|LINK
I want to check whether Session("RecordID") exists or not. If it does not exist, I want to create a new Session else I want to assign value to an existing session.
Participant
896 Points
238 Posts
Mar 02, 2012 04:49 AM|LINK
if(session["RecordID"] == NULL)
{
}
else
1596 Points
378 Posts
Mar 02, 2012 05:02 AM|LINK
Try
string val=Session["RecordID"].toString();
//if null i will throw nullreference exception
//code to assing some new value
Session.Add("RecordID",newval);
catch(Exception)
//it is null
//code you want to do
1474 Points
370 Posts
Mar 03, 2012 06:29 PM|LINK
You should always avoid throwing an exception - it is better just to check if the key maps to null as Mahesh did.
30 Points
9 Posts
Mar 04, 2012 01:14 PM|LINK
if (Session["mySessionVar"] != null) {
OutputLabel.Text = (String)Session["mySessionVar"];
in VB:
if Session["mySessionVar"] Not null then
OutputLabel.Text = Session["mySessionVar"] as String
end if
rpk2006
Member
631 Points
629 Posts
How to check whether session exists or not?
Mar 02, 2012 04:46 AM|LINK
I want to check whether Session("RecordID") exists or not. If it does not exist, I want to create a new Session else I want to assign value to an existing session.
Mahesh Darku...
Participant
896 Points
238 Posts
Re: How to check whether session exists or not?
Mar 02, 2012 04:49 AM|LINK
if(session["RecordID"] == NULL)
{
}
else
{
}
mishra.bhupe...
Participant
1596 Points
378 Posts
Re: How to check whether session exists or not?
Mar 02, 2012 05:02 AM|LINK
Try
{
string val=Session["RecordID"].toString();
//if null i will throw nullreference exception
//code to assing some new value
Session.Add("RecordID",newval);
}
catch(Exception)
{
//it is null
//code you want to do
}
Echo88
Participant
1474 Points
370 Posts
Re: How to check whether session exists or not?
Mar 03, 2012 06:29 PM|LINK
You should always avoid throwing an exception - it is better just to check if the key maps to null as Mahesh did.
chalakiblog
Member
30 Points
9 Posts
Re: How to check whether session exists or not?
Mar 04, 2012 01:14 PM|LINK
if (Session["mySessionVar"] != null) {
OutputLabel.Text = (String)Session["mySessionVar"];
}
in VB:
if Session["mySessionVar"] Not null then
OutputLabel.Text = Session["mySessionVar"] as String
end if