Reputation: 751
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.
Upvotes: 0
Views: 109
Reputation: 62290
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();
}
}
Upvotes: 1
Reputation: 1659
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).
Upvotes: 0
Reputation: 7449
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.
Upvotes: 5