ASP.NET - Session Object reference not set to an instance of an object

Asked By Daniel on 16-Aug-15 01:54 PM
 Hi!I have 2 type of registration one is customer another one is consultant,If someone is loggin but he has register as a customer means if he tried to click on a link of consultant,the system should tell him that is not a consultant as shown in the code bellow:This is a generic handler I have created to avoid that.But I am getting this error:<<Object reference not set to an instance of an object>>

 public void ProcessRequest (HttpContext context) {
        if (HttpContext.Current.Session["UserId"] != null)
        {
            ProductInfo _ProductInfo = new ProductInfo();
            _ProductInfo.UserId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
            var _ProductList = _ProductInfo.GetServiceByUserId();
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            HelperFunctions.InitializeCulture();
            var result = new
            {
                iTotalRecords = _ProductList.Count(),
                iTotalDisplayRecords = _ProductList.Count(),
                aaData = _ProductList.Select(p => new object[] { p.SN, p.ProductName, p.CategoryName, string.Format("<a href=\"#\" title=\"{0}\" class=\"product-view\">{0}<a>|{2}<a href=\"#\" title=\"{1}\" class=\"delete-icon product-delete\">{1}</a>", Resources.Language.View, Resources.Language.Delete, !p.Approved ? string.Format("<a href=\"#\" title=\"{0}\" class=\"product-edit\">{0}</a>|", Resources.Language.Edit) : string.Empty), p.ProductId })
            };
            var json = serializer.Serialize(result);
            context.Response.ContentType = "text/plain";
            context.Response.Write(json);
        }
        else {
            context.Response.Write(false);
            context.Response.Write("You are not register as a consultant to view the service page.</br>");
             
        }
            
    }

and the GetServiceByUserId() method is defined like this,if need to know what is returning:

 public IEnumerable<ProductInfo> GetServiceByUserId()
    {
        using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), "GetServiceByUserId", UserId))
        {
            if (sdr.HasRows)
            {
                 
                while (sdr.Read())
                { 
                    yield return new ProductInfo
                    {
                        SN = Convert.ToInt32(sdr["SN"]),
                        ProductId = Convert.ToInt32(sdr["ProductId"]),
                        ProductName = Convert.ToString(sdr["ProductName"]),
                        CategoryName = Convert.ToString(sdr["CategoryName"]),
                        Approved = Convert.ToBoolean(sdr["Approved"])
                    };
                }
            }
        }
    }

can help fixing this issue.
 
Robbe Morris replied to Daniel on 05-Oct-12 11:45 AM
Use the "context" passed into the ProcessRequest handler not the main one you are using now.

 if (context.Session["UserId"] != null)
Daniel replied to Robbe Morris on 05-Oct-12 12:56 PM
Hi Morris!It is giving me the error.I have changed like u said:
 public void ProcessRequest (HttpContext context) {
        if (context.Session["UserId"] != null)
        {
            ProductInfo _ProductInfo = new ProductInfo();
            _ProductInfo.UserId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
            var _ProductList = _ProductInfo.GetServiceByUserId();
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            HelperFunctions.InitializeCulture();
            var result = new
            {
                iTotalRecords = _ProductList.Count(),
                iTotalDisplayRecords = _ProductList.Count(),
                aaData = _ProductList.Select(p => new object[] { p.SN, p.ProductName, p.CategoryName, string.Format("<a href=\"#\" title=\"{0}\" class=\"product-view\">{0}<a>|{2}<a href=\"#\" title=\"{1}\" class=\"delete-icon product-delete\">{1}</a>", Resources.Language.View, Resources.Language.Delete, !p.Approved ? string.Format("<a href=\"#\" title=\"{0}\" class=\"product-edit\">{0}</a>|", Resources.Language.Edit) : string.Empty), p.ProductId })
            };
            var json = serializer.Serialize(result);
            context.Response.ContentType = "text/plain";
            context.Response.Write(json);
        }
        else {
            context.Response.Write(false);
            context.Response.Write("You are not register as a consultant to view the service page.</br>");
             
        }
            
    }
Robbe Morris replied to Daniel on 05-Oct-12 01:06 PM
Gotta change it here too:

 _ProductInfo.UserId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);

For future reference, it is helpful if you post what line the error occurs on.
Daniel replied to Robbe Morris on 05-Oct-12 01:34 PM
Morris!I got the same problem.This the code 
01.public void ProcessRequest (HttpContext context) {
02.    if (context.Session["UserId"] != null)
03.    {
04.      ProductInfo _ProductInfo = new ProductInfo();
05.      _ProductInfo.UserId = Convert.ToInt32(context.Session["UserId"]);
06.      var _ProductList = _ProductInfo.GetServiceByUserId();
07.      var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
08.      HelperFunctions.InitializeCulture();
09.      var result = new
10.      {
11.        iTotalRecords = _ProductList.Count(),
12.        iTotalDisplayRecords = _ProductList.Count(),
13.        aaData = _ProductList.Select(p => new object[] { p.SN, p.ProductName, p.CategoryName, string.Format("<a href=\"#\" title=\"{0}\" class=\"product-view\">{0}<a>|{2}<a href=\"#\" title=\"{1}\" class=\"delete-icon product-delete\">{1}</a>", Resources.Language.View, Resources.Language.Delete, !p.Approved ? string.Format("<a href=\"#\" title=\"{0}\" class=\"product-edit\">{0}</a>|", Resources.Language.Edit) : string.Empty), p.ProductId })
14.      };
15.      var json = serializer.Serialize(result);//Serialize result to jSon format
16.      context.Response.ContentType = "text/plain";//Data return type is JSon here
17.      context.Response.Write(json);//Returns Json
18.    }
19.    else {
20.      context.Response.Write(false);
21.      context.Response.Write("You are not register as a consultant to view the service page.</br>");
22.        
23.    }
24.       
25.  } I got the error on the line 02.
Robbe Morris replied to Daniel on 05-Oct-12 02:06 PM
Ah, then you probably are not implementing the proper interface for Session in your http handler:

public MyHttpHandlerClass : IHttpHandler, IRequiresSessionState


Daniel replied to Robbe Morris on 06-Oct-12 02:48 AM
Thanks Morris!