dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » ASP.NET »

Redirecting to default page in finally block


Posted Date: 19 Jun 2012      Posted By:: S.N.V.Rama Krishna     Member Level: Silver    Member Rank: 1499     Points: 2   Responses: 5



i want redirect to default error page.aspx in finally block
i have written like
try
{ Logic}
catch(Exception ex)
{string s=ex.message}
finally
{
Responce.Redirect("ErrorPage.aspx");
}

even though there is no error it is going to finally.




Responses

#676254    Author: Ravindra Gaurana      Member Level: Gold      Member Rank: 179     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

hi

try this code ...


protected void Application_Error(object sender, EventArgs e)
{
Server.Transfer("Errorpage.aspx");
}


Thanks & Regards
Ravindra Gaurana
Dotnetravindera@gmail.com
Catch Me dotnetspider



 
#676258    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

Did you got any error? its working fine for me here same like your code

Regards
N.Ravindran
Your Hard work never fails



 
#676291    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Whatever you put it in the finally block will execute regardless of whether an exception/error was thrown and handled by the try/catch block.
You can redirect to your custom error page whenever you get an error from your application. Please check below web.config setting you have to do in this case.


<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="myError.aspx"/>
</system.web>
</configuration>


If you define the error page in web.config then it is for whole application. In all the situations page will be redirected to the error page mentioned in the web.config.

If you need different error page for different page then you can declare the error page details in you page directive like below,


<%@Page language="C#" Codebehind="Test.aspx.cs"errorPage="YourErrorPage.aspx" AutoEventWireup="false"%>


Let me know if you need any assistance in this regard.


Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy






 
#676298    Author: Pawan Awasthi      Member Level: Diamond      Member Rank: 4     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hai Rama,
If there is no error then also it will execute the finally block. Finally is the block which always executes whether the error is there to not in your try block.
So due to this reason, we always keep the code which must be executed whether the error is there or not. The code like close the connection, dispose the objects usually write in the finally block.
If you want to redirect in case of error then you need to keep the settings in web.config file so that when the error will come it will redirect to the particular page.

<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="ErrorPage.aspx"/>
</system.web>
</configuration>

Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc



 
#676321    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

What ever the case is weather there is any error or not in the code if the Finally block is specified the code written inside that must be executed.

So its not the case that in case of exception or error only it will be executed. Finally block always get executed.

Place the Redirect statement in the Exception block if you want to execute it only when there is some error in the code.

Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How to add a Doc Library to a Web part in sharepoint 2010 through code
Previous : Database structure on electronic records management system
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.