1
votes

Good morning,

I have a registration form which adds the user to the system. For example when the admin enters a letter instead of a date in the date of birth, an exception occurs. I did the following code in the web.config:

"customErrors mode="On" defaultRedirect="Error.aspx""

How the try and catch function will redirect the admin to the error.aspx when the error occurs?

3
isnt it easier to make it failsafe? (if dateofbirth != the form of a DateTime dont add.) - Moonlight
rethrow the exception in catch block - Brijesh Mishra

3 Answers

1
votes

Since you have captured this exception, it will not redirect to the error page. If you want to do so, please try to rethrow the exception.

catch (Exception)
{
   // your custom code
   throw;
}
0
votes

It sounds like you need to catch the exception then do a manual redirect (Response.Redirect) to your error page. You can pass the exception to your error page in a session variable or application variable.

0
votes

Unless you do something, the admin page will NOT redirect to error.aspx if you catch the exception yourself.

it will only redirect for unhandled exceptions.

update From your comments, it seems you are better of creating some kind of validation on your page together with a validationsummary instead of relying on a redirect to the Error page. You want two things that cannot go together.

<asp:CompareValidator 
    id="dateValidator" runat="server"  
    Type="Date" 
    Operator="DataTypeCheck" 
    ControlToValidate="txtDatecompleted"  
    ErrorMessage="Please enter a valid date."> 
</asp:CompareValidator>