Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2008, 06:00 PM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in Convert.ToInt

Good evening,



I have an user custom control that a textbox, a regular expression validator for the text in the textbox to be an integer. The problem is, when I try to convert the number form the textbox it gives me an error: "Exception Details: System.FormatException: Input string was not in a correct format."



My control:


<tr>
    <td align="right"><asp:Label ID="lblName" runat="server" Text="Label"></asp:Label></td>
    <td align="left"><asp:TextBox ID="value" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ControlToValidate="value" ID="reqValue" runat="server" ValidationGroup="ad" ErrorMessage="You must input a numeric value" Text="*" CssClass="validation"></asp:RequiredFieldValidator>
       <asp:RegularExpressionValidator ControlToValidate="value" ValidationExpression="^[0-9]*$" ValidationGroup="ad" ID="regValue" runat="server" ErrorMessage="You must input a numeric value"></asp:RegularExpressionValidator>


    </td>
</tr>


My code-behind:


public void Insert(int idad)
{
     bd.InsertValueValue(idad, Convert.ToInt16(value.Text), idtp);
}


 And the complete error:

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2755599
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +112
   System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info) +25
   System.Convert.ToInt16(String value) +68



Any suggestion is appreciated :) (the problem is not in the name of the TextBox, I've tried with other names)



Thanks in advance
 
Old January 29th, 2008, 08:34 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First, be as it may, you shouldn't use keywords as object names, it needlessly makes things confusing for people that look at your code other than you.

The Format exception can be thrown if the value you are passing in == string.Empty or, obviously, it is not a value that can be converted to an Int.

Have you steped through the code? Have you looked at the value of idad as your app runs?

Also, do you realize that by calling Convert.ToInt16 you are really converting the value to a short data type and not an integer?

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert ???? bobolov Classic ASP Basics 2 June 15th, 2006 12:58 PM
Problem with convert dataset to xml ram_kimi ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 06:50 AM
Problem in convert Dataset to XML ram_kimi XML 0 May 19th, 2006 05:30 AM
Report File Format Convert Problem wscheiman Access VBA 1 January 9th, 2006 11:36 AM
Date convert Problem ppenn Access VBA 3 September 13th, 2004 02:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.