It is clear that member variables of an ASP.NET page should lose scope every time the page is refreshed.
I have the following code to display a grid of "Alerts" with edit capabilities:
public partial class Alerts : System.Web.UI.Page
{
private _WikiSyntaxEngine;
protected void Page_Load(object sender, EventArgs e)
{
_WikiSyntaxEngine = new WikiSyntaxEngine();
}
protected void gvAlerts_RowDataBound(object sender, GridViewRowEventArgs e)
{
var alert = (DataModel.Alert)(e.Row.DataItem);
// Below is the only line of interest, because only when published
// to the server would it throw an exception...
string html = _WikiSyntaxEngine.ConvertWikiTextToHTML(alert.Description);
}
}
It was only on the development server to which I published did this code fail (i.e. throws a NullReferenceException
because _WikiSyntaxEngine
was null
) during an update to the row.
However, on my local machine the code runs just fine - the row is updated and the grid refreshes as expected.
I have since fixed the code to make the WikiSyntaxEngine
a singleton, or the way it should have been designed from the beginning because it shouldn't have to be instantiated everywhere it is used.
My question then, is how can the Page_Load
event be called before gvAlerts_Databound()
on my local machine (as expected), but not called on the development machine resulting in the NullReferenceException
for _WikiSyntaxEngine
?