Solved

input string was not in a correct format?

Posted on 2012-04-03
4
406 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:aspnet-scotland
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 500 total points
ID: 37801463
Hi aspnet-scotland,

Try this

if(!string.IsNullOrEmpty(tbxHowMany.Text))
{
ql.Learners = Convert.ToInt32(tbxHowMany.Text);
}

You may also want to put a validation for numeric input on the Textbox.

Regards,
Chinmay.
0
 

Author Comment

by:aspnet-scotland
ID: 37801502
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

0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37801686
Sorry... my bad... was Tired n Hungry.. :D
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37804134
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now