The exception "Object reference not set to an instance of an object" could occur when you are trying to use a session variable within a WebMethod in a ASP.NET WebService.
An example would be as below
[WebMethod]
public string HelloWorldGetName() {
Session["name"] = "xx";
string a = Session["name"].ToString();
return a;
}
This method will throw the above exception. To avoid the error you need to enable session state inside WebMethod
[WebMethod(EnableSession=true)]
public string HelloWorldGetName() {
Session["name"] = "xx";
string a = Session["name"].ToString();
return a;
}
If you do not want to use Sessions inside the WebMethod then leave it as default. Default setting is EnableSession=false.
If you Enable Session state WebMethod would be bit more time consuming when it comes to the method execution.
Sunday, December 2, 2012
Object reference not set to an instance of an object: When using Session in WebMethod
Subscribe to:
Post Comments (Atom)
No comments:
Write comments