Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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
Updated 5-May-16 20:44pm
v3

Try this
C#
string str=string.empty;
str=convert.tostring(session["name"]);


Thanks
 
Share this answer
 
Comments
[no name] 6-Jan-13 1:29am    
*****
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[^]
 
Share this answer
 
Comments
[no name] 16-Jan-13 7:01am    
string str=convert.tostring(session["name"]);
if you are getting error null object or object reference not set to...

then
C#
string str = "";
if (session["name"] != null)
{
    str = session["name"].ToString();
}

Happy Coding!
:)
 
Share this answer
 
session return object type.
try it-> string str=session["name"].ToString();
 
Share this answer
 
Comments
Member 9511889 5-Jan-13 5:12am    
i have try this also.but give me rror.
Try this:
string str=session["name"].ToString();
 
Share this answer
 
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"]);
 
Share this answer
 
Hi,

Session always return Object type.

It will only get error if your object is null.
The Error will :
C#
object reference is not set to an instance of an object 


To solve this kind of scenario

C#
string Name= (Session["Name"] ?? "").ToString();
 
Share this answer
 
I usually use a Session-backed property when working with a session object

C#
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.

C#
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;
}
 
Share this answer
 
v3
Try this !!!

C#
string str = "";
if (session["name"] != null && session["name"].ToString() != "")
{
    str = session["name"].ToString();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900