Assume the following code:
using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand comm = new SqlCommand(...)) { .. do stuff .. if(condition) Response.Redirect('somepage.aspx'); } }
Will the Response.Redirect() exit from the using blocks cause it to dispose all connections?
Or, alternatively, is there any way to exit a using block that won’t cause disposal?
EDIT: I don’t want to exit without disposal. I want to be aware of any pitfalls that would cause it not to work. — Barring crashes of course, but then I’m pretty sure all objects are disposed –the hard way– in that situation
I’ve accepted an answer, which essentially says ‘I don’t know’ but it’s a very well researched ‘I don’t know’
For the mean time, I’m going to assume that Response.Redirect aborts the using statement and code with that in mind. — Until proven otherwise.
From http://msdn.microsoft.com/en-us/library/aa973248.aspx
Yes, it does not directly address the Using statement, but it is a common enough programming practice to be aware of. Also, that article refers to SharePoint, but as SP is built on ASP.NET 2.0, I think it is still relevant.