Click here to Skip to main content
14,984,183 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Can I ask about why my output for PageError.aspx is not same as correct output below, please?:

The correct output for PageError.aspx after I entered two inputs such as 7 and 0 by using the pageerror method only (cannot use try catch method):

Sorry. One Error is encountered in this page: Attempted to divide by zero.

I still cannot get this correct output as mentioned above after I Google several times and sample source code, the PageError.aspx output still display the input for perform division with 2 blanks. Can you all explain my errors, please?

What I have tried:

I paste some these problems code here as below:

For PageError.aspx,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageError.aspx.cs" Inherits="WebApplication1.PageError" Trace="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Error Handling</title>  
    
</head>
<body>
    <form id="form1" runat="server">
<div style="text-align:center">
<p style="width:60%; padding:30px 20px 30px 20px; border:solid 3px black">
    <asp:TextBox ID="TextBox1" runat="server" Height="43px" Width="66px" 
        Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
  <img alt="" src="images/Divide.jpg" style="height: 39px; width: 65px" /> 
    <asp:TextBox ID="TextBox2" runat="server" Height="43px" Width="66px" 
        Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
 <asp:ImageButton ID="EqualButton" runat="server" Height="44px" 
        ImageUrl="~/images/EqualButton.jpg" Width="50px" 
        onclick="EqualButton_Click" />
 <asp:Label ID="Label1" runat="server" Font-Bold="True" 
        Font-Names="Comic Sans MS" Font-Size="XX-Large" ForeColor="Blue" Text="?"></asp:Label>
<br />
    <asp:Label ID="lblMessage" runat="server" Text="" Font-Names="Arial" ForeColor="Red"></asp:Label>
</p>
</div>
    </form>
</body></html>


For PageError.aspx.cs,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class PageError : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void EqualButton_Click(object sender, ImageClickEventArgs e)
        {
            double dblAns = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(TextBox2.Text);

            Label1.Text = dblAns.ToString();
        }

        void Page_Error()
        {
           Response.Write("Sorry <br /><br />" + "One error is encountered in this page: " + Server.GetLastError().Message + "");

           Server.ClearError(); //comment this line to see the difference  
        }

        //protected void Page_Error(object sender, EventArgs e)
        //{
        //    Response.Write("Error: " + Server.GetLastError().Message + "");
        //    Server.ClearError();
        //}

        //    void Page_Error()
        //    {
        //        if(Server.GetLastError() != null)
        //        { 
        //           Exception objError = Server.GetLastError().GetBaseException();

        //           string strError = "<p><h1>Sorry<br /><br />" +
        //                             "One error is encountered in this page: " + objError.Message.ToString() + "</h1></p>";

        //           Response.Write(strError.ToString());
        //        }

        //        // Clear the error from the server.
        //        // Server.ClearError();
        //    }
        //}
    }
}


For Web Config,

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
  <system.web>
    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime targetFramework="4.6.1"/>
           <!--<customErrors mode="On" defaultRedirect="ErrorPages/PageError.htm">
                 <error statusCode="404" redirect="ErrorPages/FileNotFound.htm"/>
           </customErrors>-->
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.webServer>
    <httpErrors errorMode="Custom" defaultResponseMode="File">
      <clear/>
      <error statusCode="404" path="ErrorPages\FileNotFound.htm"/>
    </httpErrors>
  </system.webServer>
</configuration>
Posted
Updated 11-Oct-20 3:55am
v5

1 solution

C#
double dblAns = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(TextBox2.Text);

You should not use code like that as it allows any garbage to be entered into your program. Capture each field separately and check that the entered values are valid before doing anything else. Something like:
C#
double dividend;
if (!Double.TryParse(TextBox1.Text, out dividend))
{
    // deal with invalid input
}
double divisor;
if (!Double.TryParse(TextBox2.Text, out divisor))
{
    // deal with invalid input
}
if (divisor == 0)
{
    // deal with divide by zero
}
double quotient = dividend / divisor;
   
Comments
[no name] 11-Oct-20 10:23am
   
Ok. I got it. Thanks.

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