Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
i have to asign session value to string datatype.
For Ex:
string str=session["name"].ToString();
 
but this will give me error.what is problem?
Posted 4-Jan-13 23:00pm
Edited 4-Jan-13 23:46pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this
string str=string.empty;
str=convert.tostring(session["name"]);
 
Thanks
  Permalink  
Comments
Turbo_23 at 6-Jan-13 1:29am
   
*****
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Session value get like this you was write code.
firstly say what is error.
as it give null value.
YOU CAN ALSO USED :-
 
string firstName = (string)(Session["First"]);
OR
string str=session["name"].ToString();
for this you need to check your session variable exist or not or expired, abonden.
SO YOU NEED TO READ FOLLOWING LINK:=
http://msdn.microsoft.com/en-us/library/03sekbw5(v=vs.100).aspx[^]
  Permalink  
Comments
kanjolia gopal at 16-Jan-13 7:01am
   
string str=convert.tostring(session["name"]);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if you are getting error null object or object reference not set to...
 
then
string str = "";
if (session["name"] != null)
{
    str = session["name"].ToString();
}
Happy Coding!
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

session return object type.
try it-> string str=session["name"].ToString();
  Permalink  
Comments
Member 9511889 at 5-Jan-13 5:12am
   
i have try this also.but give me rror.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:
string str=session["name"].ToString();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi,
 
I would suggest that the best way would be to use Convert.ToString() instead of the .ToString() method. This is because by default, the .ToString() method doesnot have the capability to handle null values, hence this would throw an error when you try to convert null values to string. On the other hand Convert.ToString() method has the default capability of handling null values & hence won't throw any error -
 
string str = Convert.ToString(session["name"]);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

I usually use a Session-backed property when working with a session object
 
public string MyVariable
{
    get
    {
        object temp = Session["MyVariable"];
        return temp == null ? string.Empty : temp as string;
    }
    set
    {
        Session["MyVariable"] = value;
    }
}
 
Now I don't have to remember what the Session variable name is and I have intellisense support for it. If at some point the Session variable is null then it will return a default value, which in this case is an empty string. The default can be changed depending on what the actual data type is you're working with. Here's a quick example of how you would use this in your code.
 
public string MyVariable
{
    get
    {
        object temp = Session["MyVariable"];
        return temp == null ? string.Empty : temp as string;
    }
    set
    {
        Session["MyVariable"] = value;
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    MyVariable = "This is some text.";
    TextBox1.Text = MyVariable;
}
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Try this !!!
 
string str = "";
if (session["name"] != null && session["name"].ToString() != "")
{
    str = session["name"].ToString();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Hi,
 
Session always return Object type.
 
It will only get error if your object is null.
The Error will :
object reference is not set to an instance of an object 
 
To solve this kind of scenario
 
string Name= (Session["Name"] ?? "").ToString();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 598
1 George Jonsson 268
2 OriginalGriff 215
3 CPallini 210
4 PIEBALDconsult 155
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,263
2 CPallini 4,750
3 George Jonsson 3,227
4 Gihan Liyanage 2,487


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100