Link to home
Start Free TrialLog in
Avatar of aspnet-scotland
aspnet-scotlandFlag for United Kingdom of Great Britain and Northern Ireland

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:

<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>

Open in new window


The database field is of type integer and is set to null.

The code behind looks like:

ql.Learners = Convert.ToInt32(tbxHowMany.Text);

Open in new window


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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aspnet-scotland

ASKER

Do I not already have that validation...

<asp:CompareValidator ID="cv" runat="server" ControlToValidate="tbxHowMany" Type="Integer" 
             Operator="DataTypeCheck" ErrorMessage="Value must be an integer" />

Open in new window

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.