Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

input string was not in a correct format?

Posted on 2012-04-03
4
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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