none
Convert text box value to UInt32 error

    Question


  • I have an app in C# 2005

    Text box contains a 12 digit number.

    But when I pass this text box value, raise an error.

    Like

      DisplayEnroll(Convert.ToUInt32(TextBox.Text));

    error is
       "Value was either too large or too small for a UInt32"

    How can I solve this issue ?
    Monday, July 16, 2012 9:26 AM

Answers

All replies

  • Hi,

    If you check UInt32.MaxValue (it's 4 294 967 295), you can see that the 12 digit integer doesn't fit to UInt32. You can use UInt64 instead.


    Monday, July 16, 2012 9:29 AM
  • In function definition how can I declare these parameter

    As Uint or Uint64 ?

    Monday, July 16, 2012 9:53 AM
  • UInt means UInt32, so you should declare UInt64.

    But be aware, that UInts are not CLS-compliant, so maybe it would be better to use Int64 or Decimal instead.

    • Marked as answer by ssa2010 Thursday, July 19, 2012 6:06 AM
    Monday, July 16, 2012 10:00 AM
  • Validate your textbox.Text with following code whether it is numeric or contains alphabets

    bool Validate(string inputText)
            {
                return inputText.All(Char.IsDigit);
            }


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, July 16, 2012 10:05 AM
  • Uint32 Range is

    Range
    0 to 4,294,967,295

    Your Question is In function definition how can I declare these parameter As Uint or Uint64 ?

    Find the Example below

    public void TstFunction (UInt64 Val)

    { /

    /Your Code

    }


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Monday, July 16, 2012 10:11 AM
  • Hi,

    This error occur when size of the assignment variable is smaller then current value.

    instead of using UInt32 datatype please use decimal.

    example

    Decimal  i=Convert.ToDecimal(textBox.Text);

    so 

    • change DisplayEnroll(Convert.ToUInt32(TextBox.Text));  to DisplayEnroll(Convert.ToDecimal(TextBox.Text)); 
    • change the data type of DisplayEnroll(decimal) parameter.

    Thanks


    Mark Answered, if it solves your question and Vote if you found it helpful.

    Monday, July 16, 2012 10:34 AM
  • The problem is that a 12 character number is too big to be stored in a UInt32 variable.

    This code can help you to understand what is happening.

                var maxUInt32 = UInt32.MaxValue;

                //4294967295

                var maxUInt64 = UInt64.MaxValue;

                //18446744073709551615

                UInt32 myUInt32;

                if (!UInt32.TryParse(textBox1.Text, out myUInt32))

                    throw new ArgumentException(string.Format("Value {0} is not a valid UInt32", textBox1.Text));

                UInt64 myUInt64;

                if (!UInt64.TryParse(textBox1.Text, out myUInt64))

                    throw new ArgumentException(string.Format("Value {0} is not a valid UInt64", textBox1.Text));

    Regards,

    Sergio
    http://zsvipullo.blogspot.it/

    Monday, July 16, 2012 3:12 PM
  • Hi,

    This error occur when size of the assignment variable is smaller then current value.

    instead of using UInt32 datatype please use decimal.

    example

    Decimal  i=Convert.ToDecimal(textBox.Text);

    so 

    • change DisplayEnroll(Convert.ToUInt32(TextBox.Text));  to DisplayEnroll(Convert.ToDecimal(TextBox.Text)); 
    • change the data type of DisplayEnroll(decimal) parameter.

    Thanks


    Mark Answered, if it solves your question and Vote if you found it helpful.

    Hi M_06

    It works.

    This is my test results.

    Convert.ToUInt32 - supports 10 digit number

    Convert.ToUInt64 - supports 20 digit number

    Convert.ToDecimal - supports 29 digit number

    and

    long.Parse - supports 19 digit number

    But Convert.ToDecimal is same as that of decimal.Parse

    and Convert.ToUInt32  equals UInt32.Parse

    and Convert.ToUInt64 equals UInt64.Parse

    My doubt is what is the difference between these two conversions ?

    Tuesday, July 17, 2012 4:22 AM
  • Hi ssa2010 ,

    I’d like to take Convert.ToInt32  and  Int32.Parse  for example ,

    Convert.ToInt32   : converts an object to an integer and returns 0 if the value was null.

    Int32.Parse  : convert a string to an integer and throws an exception if the value wasn't able to  convert

    Please have a look at these links , this will be helpful to you : http://www.codeproject.com/Articles/32885/Difference-Between-Int32-Parse-Convert-ToInt32-and

    http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ssa2010 Thursday, July 19, 2012 5:37 AM
    Thursday, July 19, 2012 2:17 AM
  • Hi Lisa Zhu

    It is very helpful

    Thursday, July 19, 2012 6:08 AM