Solved

input string was not in a correct format?

Posted on 2012-04-03
4
405 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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