HardCode Posted August 18, 2004 Posted August 18, 2004 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? Quote
PWNettle Posted August 18, 2004 Posted August 18, 2004 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 Quote
jspencer Posted August 19, 2004 Posted August 19, 2004 If you do this, it will process the rest of your code: Try response.redirect("home.aspx") Catch ex As System.Exception Finally oConn.Close End Try Quote
Arch4ngel Posted August 19, 2004 Posted August 19, 2004 If you do this... it will probably have the same resultResponse.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. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
*Gurus* Derek Stone Posted August 19, 2004 *Gurus* Posted August 19, 2004 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. Quote Posting Guidelines
Arch4ngel Posted August 19, 2004 Posted August 19, 2004 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 ??????? Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
HardCode Posted August 19, 2004 Author Posted August 19, 2004 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)? Quote
Arch4ngel Posted August 19, 2004 Posted August 19, 2004 Alternative and better solution I have one solution. It shall be done like this. 1 redirect only. Only the parameter change. '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) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.