Code after "Response.Redirect"

HardCode

Freshman
Joined
Apr 2, 2004
Messages
49
Supposed in Page_Init you have a Response.Redirect statement. Will any code after that statement (in Page_Init) still execute after/during/right-before the redirect? I am asking because I open a SqlConnection object, then check some conditions. Most conditions invoke a Response.Redirect, and I want to make sure that I can still get to the line "oConn.Close". Do I need to put it before every Response.Redirect or will the rest of the Sub still execute?
 
There are two Response.Redirect methods. One causes immediate redirection (and your remaining code will not fire), the other allows you to pass in a bool in addition to the redirect URL to indicate whether or not the current page execution should terminate.

Check out MSDN for specifics and details.

Paul
 
If you do this, it will process the rest of your code:

Code:
Try
       response.redirect("home.aspx")

Catch ex As System.Exception

Finally
       oConn.Close
End Try
 
If you do this... it will probably have the same result
Visual Basic:
Response.Redirect("Home.aspx", false)
oConn.Close()

Doing too much Try... Catch... Finally is memory or CPU consumming. You can see your app slow down when you use too much of them. Try Catch shall be avoid as most as possible.

As an exemple... you don't do a try catch on a integer division and catch a DivisionByZeroException... you can avoid this type of error. The more you can avoid TryCatch and the faster your app will be.

Really poor practice of using try catch everywhere.
 
You need to refactor your code. There's no reason why Response.Redirect() needs to come before a call to SqlConnection.Close(). Close the connection and then redirect.
 
One cookie for Derek and one slap for the rest of us :p

After seeing Derek's post... I'm asking myself... why a redirect before a closing connection ???????
 
Derek Stone said:
You need to refactor your code. There's no reason why Response.Redirect() needs to come before a call to SqlConnection.Close(). Close the connection and then redirect.

This was actually the point of the question, because I am doing 5 independent verifications in the Page_Init. So, if the first one fails, then redirect to fail1.aspx. If it passes and the second one fails, redirect to fail2.aspx, and so on. The main reason I am not using one fail.aspx and passing the failure message is because my code redirects to an on-line software package and doesn't justify the development. I am only writing one page :) I know I can put a oConn.Close 5 times, but I was wondering about the rest of the code executing because I in fact do have a try...catch...finally construct, with the oConn.Close in the Finally part, and I would naturally prefer oConn.Close once instead of 5 times :)

Now, just to verify, will the Finally part still execute with Response.Redirect("page.aspx") or must I additionally use the parameter of "false" in Response.Redirect("page.aspx", false)?
 
Alternative and better solution

I have one solution. It shall be done like this. 1 redirect only. Only the parameter change.

Visual Basic:
'After your connection openned
Dim bTest1, bTest2 as Boolean
Dim pageRedirect as String
pageRedirect = "default.aspx"
 
If( Not bTest1 ) Then
pageRedirect = "fail1.aspx"
End If
If( Not bTest2 ) Then
pageRedirect = "fail1.aspx"
End If
'and so on...
'Close your connection here.
Response.Redirect( pageRedirect)
 
Back
Top