RangeValidator in WebForms Page Fails with error

98 Views Asked by At

I have been tasked with maintaining an older webforms application without much training or experience.

I was asked to add a new field to an existing table and expose it in the web page. Here's the code to do that, which I modeled on other existing fields and code.

 <asp:TextBox ID="PortableBarrelCompressionValuetextBox" 
    runat="server" 
    SkinID="FVTextBox" 
    Text='<%# Bind("PortableBarrelCompressionValue") %>'  
    <asp:RangeValidator 
    ID="RangeValidatorPortableBarrelCompressionValuetextBox"
    runat="server" 
    ControlToValidate="PortableBarrelCompressionValuetextBox"
    CssClass="failureNotification" 
    Display="Dynamic"
    ErrorMessage="Whole Numbers Between 0 and 9999" 
    MaximumValue="9999"
    MinimumValue="0"
    Type="Integer" 
    Text='<%# Eval("PortableBarrelCompressionValue") %>'>
    </asp:RangeValidator>

This only works correctly when the existing value is non-null (and of course, within range). If I change a valid number, say 22, to a different valid number, 200, it saves the change as expected.

However, if the value on an existing record was null and I try to add an integer, it fails with the error message:

Value   {" is not a valid value for Int32."}    System.Exception

It also fails if there is an existing, valid, value which deleted, with the same error.

This field is not required, so I have to allow for Nulls here.

I've learned enough in the last two days to decide this is happening because the field is an integer and that the Null can't be converted to a string for the validation (at least that's what I'm assuming now).

If that is true, and this is the problem, I need to find a way to resolve that.

Am I on the right track?

Is there a way to provide a validation that will also accept nulls in this field, in addition to integers in the proper range?

Thanks in advance.

2

There are 2 best solutions below

1
kblau On

This does not throw your error

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FredWebForm.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <%--bind only used with controls that have datasource eg grid--%>
    <asp:TextBox runat="server" Text='<%# GetStatus("PortableBarrelCompressionValue") %>' ID="MyTextBox" />
    <asp:RangeValidator
        ID="RangeValidatorPortableBarrelCompressionValuetextBox"
        runat="server"
        ControlToValidate="MyTextBox"
        CssClass="failureNotification"
        Display="Dynamic"
        ErrorMessage="Whole Numbers Between 0 and 9999"
        MaximumValue="9999"
        MinimumValue="0"
        Type="Integer"
        Text='Needs to be a number between 0 and 9999'>
    </asp:RangeValidator>
</asp:Content>

Code behind

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataBind();
        }
    }

    protected string GetStatus(string theID)
    {
        return null;
    }
}
0
George Hepworth On

I finally figured out my error.

It was not in this section at all.

I didn't realize that the definition of the LingDataSource for the form has to be updated as well.

Once I added this line:

<asp:Parameter Name="PortableBarrelCompressionValue"
ConvertEmptyStringToNull="true" />

It came back to life.

Thanks for all the support.