aspnet-scotland
asked on
input string was not in a correct format?
Hi,
I have created an .aspx web page as a front-end form to my db table with the below code for an input field:
The database field is of type integer and is set to null.
The code behind looks like:
The above field is not a required field, hence the null reference. However, upon testing the form the error "input string was not in a correct format" flags on submit. How can I alter my code to allow the form to accept a null value on submit?
Thanks.
I have created an .aspx web page as a front-end form to my db table with the below code for an input field:
<div class="regText">
<asp:TextBox ID="tbxHowMany" runat="server"></asp:TextBox>
<asp:CompareValidator ID="cv" runat="server" ControlToValidate="tbxHowMany" Type="Integer"
Operator="DataTypeCheck" ErrorMessage="Value must be an integer" />
</div>
The database field is of type integer and is set to null.
The code behind looks like:
ql.Learners = Convert.ToInt32(tbxHowMany.Text);
The above field is not a required field, hence the null reference. However, upon testing the form the error "input string was not in a correct format" flags on submit. How can I alter my code to allow the form to accept a null value on submit?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry... my bad... was Tired n Hungry.. :D
Do I not already have that validation...No, you do not. Please read up on the CompareValidator. From the linked page:
If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.
Also, you have not specified the value to compare against. You should either be targeting a constant value or another input field. You would use a CompareValidator to say, "this field must be less than 5," or "this field must be greater than the value entered in txtSomeOtherValue." You have neither of these.
ASKER
Open in new window