Click here to Skip to main content
16,022,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Afternoon,
Can anybody please tell this answer. Actually i am trying many time but i didn't getting actual answer and what happens behind this... please give the answer with briefly.
I have two Scenario : 1 & 2...
Scenario:1-
C#
Try{
Response.Redirect("1.aspx");
}

Catch{
Response.Redirect("2.aspx");
}

finally{
Response.Redirect("3.aspx");
}


Scenario:2-
C#
Try
{
Server.Transfer("1.aspx");
}

Catch
{
Server.Transfer("2.aspx");
}

finally
{
Server.Transfer("3.aspx");
}
Posted
Updated 11-Mar-14 22:09pm
v2
Comments
Abhishek Pant 12-Mar-14 4:07am    
I don't know why are you using "Response,redirect" in place of "Response.redirect"

by the way it doesn't/partially handle the redirection in try catch. make it out of try catch it works successfully.

I think this is the appropriate answer..

scenario 1-
The Output of this one is that it will redirect the page to 3.aspx as on try block this will throw a redirect error but after catch block the control will go to the finally block which will redirect the page to 3.aspx

Scenario 2-
In the Second case we are using Server.Transfer which will directly transfer the current page to show the contents of the page to be redirected keeping the url address as the same.. IN this case it will move through all three blocks and would show the contents of 1.aspx, 2.aspx and 3.aspx from the main page... ie., if we are transferring from main.aspx then it will show the contents of 1.aspx plus the content of 2.aspx and the content of 3.aspx in main.aspx
 
Share this answer
 
Its a Response.Redirect and not Response,Redirect.
 
Share this answer
 
What will happen is if there were any page named 1.aspx it will be redirected else
it will result in 404 page not found
in what code uve specified.


but when u try to catch server error then u can try redirecting.

and there is no point in using finally in this screnario.


ill put one best example where we use all these three

C#
 SqlConnection conn2 = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

public DataTable ExecuteSql(string sql)
       {
           DataTable dt = new DataTable();
           try
           {

               DataSet ds = new DataSet();
               conn2.Open();

               SqlDataReader rdr;
               SqlCommand cmd = new SqlCommand(sql, conn2);
               cmd.CommandTimeout = 60000;
               cmd.CommandType = CommandType.Text;
               rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
               dt.Load(rdr);
               return dt;
           }
           catch(Exception ex)
           {
               throw ex;
           }
           finally
           {
               conn2.Close();

           }
       }




C#
Exception ex = Server.GetLastError();

HttpException httpEx = ex as HttpException;

if (httpEx != null && httpEx.GetHttpCode() == 404)
{
   //do what you want for 404 errors
}
 
Share this answer
 
Comments
Pravat_Sahoo 12-Mar-14 4:05am    
Thanks Andrew for example.
But in first scenario the output is 3.aspx.
on try block this will throw a redirect error but after catch block the control will go to the finally block.
AndrewCharlz 12-Mar-14 4:21am    
finally will always be executed but in this case once page is redirected it wont go into finally
In both the scenario, 3.aspx will be the final o/p. Because finally block will always be executed whether the exception is thrown or not, it doesn't matter...

For knowing difference between Server.Transfer() and Response.Redirect() methods,
refer the below link,

Difference Between Response.Redirect() and Server.Transfer() Methods in ASP.NET[^]
 
Share this answer
 
in Order try and catch finally will be executing so 3.aspx is the output page and coming to Response.Redirect and Server.Transfer

Resposne.redirect: it redirects in the same server itself where as we can get the values of previous page

Server.Transfer: it transfers to the new server where values gets lost of old page
 
Share this answer
 
First Scenario won't Compile

In second one.The page get redirected to 1.aspx page


Both Scenario won't Compile

and i strongly suggest you to read this
Difference Between Response.Redirect() and Server.Transfer() Methods in ASP.NET[^]
 
Share this answer
 
v3
Comments
Tom Marvolo Riddle 12-Mar-14 4:47am    
Downvoting for this answer :laugh:.It cause nothing to me

Let me explain:
In both scenario Try should be try
Catch should be catch

C# is case sensitive

Response,redirect ----------- Response.Redirect;

After all ,i/we don't know whether the page exist or not.then how can i/we explain.That's why i posted a link which clearly explains the differences.It's upto you to read or not

Finally,my intention is not to discourage you and i want you to know the unknown.That's it. :)
Pravat_Sahoo 12-Mar-14 5:02am    
what you are trying to say i understood nicely. Ok thanks...But you didnot getting my questing thats why you wrote this type of answer.
scenario 1-
The Output of this one is that it will redirect the page to 3.aspx as on try block this will throw a redirect error but after catch block the control will go to the finally block which will redirect the page to 3.aspx..
Scenario 2-
In the Second case we are using Server.Transfer which will directly transfer the current page to show the contents of the page to be redirected keeping the url address as the same.. IN this case it will move through all three blocks and would show the contents of 1.aspx, 2.aspx and 3.aspx from the main page... ie., if we are transferring from main.aspx then it will show the contents of 1.aspx plus the content of 2.aspx and the content of 3.aspx in main.aspx
Tom Marvolo Riddle 12-Mar-14 5:26am    
if we are transferring from main.aspx then it will show the contents of 1.aspx plus the content of 2.aspx and the content of 3.aspx in main.aspx----Yup,you're right.

Happy coding :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900