I am using asp.net forms. There is a Page_Load
event,but is there an end event?
I have a linq datacontext created on pageload and I would like to dispose it when I am done.
I am using asp.net forms. There is a Page_Load
event,but is there an end event?
I have a linq datacontext created on pageload and I would like to dispose it when I am done.
You should probably do it on Page_Unload
Event is the last event in page life cycle. For more on page events check out this.
As Emmanuel N stated, there is Page_Unload
event. However, if you use using
, you do not need to worry about disposing DataContext
.
Here is an example.
protected void buttonSearch_Click(object sender, EventArgs e)
{
using (var context = new NorthwindDataContext())
{
var customers =
from c in context.Customers
select c;
gridViewCustomers.DataSource = customers;
gridViewCustomers.DataBind();
}
}
By the way if you are using Entity Framework, you don't have to dispose DbContext: the default behaviour is to open connection when needed and close it when done (more details).