Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (Session["Pagetype"].ToString() == "EXIST" || !String.IsNullOrEmpty(qID) || Session["Pagetype"].ToString() == "Preview")



"Object reference not set to an instance of an object" error for both session type.

if (Session["Pagetype"].ToString() == "EXIST" || Session["Pagetype"].ToString() == "Preview")



FYI - the page is not loading from the session page type. so the session state is false.
Posted

1 solution

C#
object pageType = Session["Pagetype"];
if ((!string.IsNullOrEmpty(pageType.ToString()) && 
    (pageType.ToString() == "EXIST" || pageType.ToString() == "Preview")))
     || !String.IsNullOrEmpty(qID) 


what is likely happening is that the Session["Pagetype"] is coming back null and as such you cannot take the .ToString() of a null object. you have to test it first for null

another way to write it is

XML
object pageType = Session["Pagetype"];
if (!string.IsNullOrEmpty(pageType.ToString()))
{ 
    if (pageType.ToString() == "EXIST" || pageType.ToString() == "Preview"
     || !String.IsNullOrEmpty(qID)
    {
    }
}
 
Share this answer
 
v3
Comments
bowlturner 10-Dec-13 10:34am    
Session["Pagetype"].ToString() == null

is not the same as

Session["Pagetype"] == null

if Session["Pagetype"] == null then this Session["Pagetype"].ToString() == null will throw an exception.

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