Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

input string was not in a correct format?

Posted on 2012-04-03
4
Medium Priority
?
413 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

963 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