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.
This does not throw your error
Code behind