I am receiving this error message when I debug my program.
Object reference not set to an instance of an object.
That error happens on this line:
protected void Page_Load(object sender, EventArgs e)
{
bool x = (bool)Session["IsConnectionInfoSet"];--> error here
if (IsPostBack && x)
//do something with the bool x variable
}
Postback is invoked by:
protected void btnDo_Click(object sender, EventArgs e)
{
//do something
Session["IsConnectionInfoSet"] = true;
//do something
}
This error happened in Visual Studio 2008, .NET Framework 3.5.
Can someone give me advice on how this?
The Page_Load method is always run before any event handlers. As a result, the page_load will run, find null and throw an error, all before you click handler has a chance to set this session value.
Here’s a safer way to access this session value