0

So I set up my Global.asax file to email me when an unhanded exception occurred. I get an email with exceptions that don't appear to the user, and the code appears to be working just fine at the client end and the page loads and does what is expected.

I have this code in the aspx file, which shows a checkbox based on a role or a customer number started with specific characters...

<%  if (Roles.IsUserInRole("Admin") ||
                   txtAccountNo.Text.Substring(0, 2) == "UK" ||
                   txtAccountNo.Text.Substring(0, 2) == "FR" ||
                   txtAccountNo.Text.Substring(0, 2) == "IT" ||
                   txtAccountNo.Text.Substring(0, 2) == "DK" ||
                   txtAccountNo.Text.Substring(0, 2) == "SE")
               { %>
            <label for="chkExpressDelivery"><asp:Literal ID="Literal14" runat="server" Text="<%$ Resources:LocalizedText, fieldLabel_ExpressDelivery %>"></asp:Literal><span class="small"></span></label>
            <asp:CheckBox ID="chkExpressDelivery" runat="server" Width="50px" CssClass="_checkbox _normalcheckbox" Text="&nbsp;" />
            <%} %>

now txtAccountNo is populated from a user profile property in the Page_Load event in the aspx.cs file.

the error message i receive in the email (which the client never sees) is

Index and length must refer to a location within the string. Parameter name: length

I understand the error message - that it's saying the txtAccountNo.Text is basically empty when that code runs, but what i dont understand is, the client never sees this error and the page behaves as i want it to.

BUT, if i turn on custom error messages in the web.config file, the client is directed to the custom error page, but without it, no error as far as the client is concerned.

So how can i correct this error so i can turn on my custom error pages?

I hope this makes sense! Let me know if you need any more info.

Thanks

Stuart

this code now works...

<%  if (txtAccountNo.Text.Length > 0 && (Roles.IsUserInRole("Admin") ||
               txtAccountNo.Text.Substring(0, 2) == "UK" ||
               txtAccountNo.Text.Substring(0, 2) == "FR" ||
               txtAccountNo.Text.Substring(0, 2) == "IT" ||
               txtAccountNo.Text.Substring(0, 2) == "DK" ||
               txtAccountNo.Text.Substring(0, 2) == "SE"))
           { %>
        <label for="chkExpressDelivery"><asp:Literal ID="Literal14" runat="server" Text="<%$ Resources:LocalizedText, fieldLabel_ExpressDelivery %>"></asp:Literal><span class="small"></span></label>
        <asp:CheckBox ID="chkExpressDelivery" runat="server" Width="50px" CssClass="_checkbox _normalcheckbox" Text="&nbsp;" />
        <%} %>
Stuart
  • 1,544
  • 5
  • 29
  • 45

1 Answers1

1

There are errors that are never shown to the client. It happened to me while I was using an AJAX control in a page and the client had a certain IE version. The client was not affected by it, but it kept sending error mails through Emah.
When activating custom errors, your application or the IIS server may catch the error and interprete it in a certain way. Something like this: mvc3 error on webserver, only get generic response Maybe that is why the redirect occurs.
As for the source of the error (txtAccountNo.Text being empty): if the code is ran during a certain moment in the page event lif cycle (e.g. initialization) there is no data in the control. To avoid the error you can add another condition to the if clause and check for txtAccountNo.Length > 0.

Community
  • 1
  • 1
CristisS
  • 1,103
  • 1
  • 12
  • 31
  • Thanks! I'll give that a go and let you know if it works! I seem to have a few errors like this where there's code in the aspx page relating to fields that are populated in the aspx.cs file. – Stuart Nov 07 '12 at 09:50