Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
Posted

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

Posted

If you do this... it will probably have the same result

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.

"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

Posted

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 ???????

"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

Posted
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)?

Posted

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)

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...