Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form that adds and subtracts numbers on textbox text change. If I put in a number like 2181003000 - 899763000 I get this error, "Value was either too large or too small for an Int32.". How can I fix this?

C#
protected void TextBoxNPRNA_TextChanged(object sender, EventArgs e)
   {
       int a = Convert.ToInt32(TextBoxTUNA.Text);
       int b = Convert.ToInt32(TextBoxETRNA.Text);
       int c = Convert.ToInt32(TextBoxNPRNA.Text);
       TextBoxTNA2.Text = Convert.ToString(a + b + c);
       TextBoxTR.Focus();
   }
Posted
Updated 3-Feb-20 23:54pm
Comments
PIEBALDconsult 9-Jul-14 10:57am    
I would have thought you'd get:
"System.FormatException: Input string was not in a correct format.\r\n at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n at System.Convert.ToInt32(String value)\r\n at PlayGround.Form1.Form1_Load(Object sender, EventArgs ea)"
And I addition, not subtraction.

Why do people insist on doing this?

Never use Convert to change the type of user input: use TryParse instead. That way when the user makes a mistake - as they do all the time - you can detect it before it becomes a problem, and tell them exactly what they have to do to fix it:
C#
int a;
if (!int.TryParse(TextBoxTUNA.Text, out a))
   {
   // Report problem to user
   return;
   }
 
Share this answer
 
v2
Comments
Computer Wiz99 9-Jul-14 11:12am    
OriginalGriff, So I have this: int a = Convert.ToInt32(TextBoxTUNA.Text); and I changed it to this:
int b = Convert.ToInt64(long)(TextBoxTL.Text);. I get and error between (long)(TextBoxTL.Text). How can I fix this?
Andreas Gieriet 9-Jul-14 12:04pm    
Did you understand what OriginalGriff explained to do? He said [...] ***never*** use Convert [...]. You seem to be ignorant to the suggested help...
Andi
Computer Wiz99 9-Jul-14 12:11pm    
I don't understand how to use that method. I am not trying to store anything. Just have it to where if a user enters a number like 2181003000 into a textbox and 899763000 into another textbox to subtract them together to get an answer that an error will not fire. I did do some research on what he show me but still didn't make heads or tails of it.
Andreas Gieriet 9-Jul-14 12:22pm    
Have you looked up "C# tryparse" in google?
Think aboout the task: what if a user enters crap? You have to handle this. In your case, the numbers are too large to fit into a 32 bit integer (that's what int32 or int are). The result is an exception (an error that says that the system can not recover from bad entry since you did not provide the means to handle error like the above gracefully.

So, take the larger integer type, e.g. int64 (this is the C# long type) and use TryParse as suggested above. TryParse returns true if "no crap" was entered and if the value fits into 64 bits. It returns false otherwise. To allow the conversion of TryParse to store the value into a variable, the variable has to be passed as out parameter to that function. In you case it must be long and *not* int.

Look up the methods in google. If it is too high level, then I doubt you can produce a meaningful function at all. Sorry for being so bluntly.
Andi
PS: Looking at you history of questions, I suggest that you work through a basic tutorial on C#. There are many out there in the internet. Good luck!
Computer Wiz99 9-Jul-14 12:38pm    
Ok. That makes more sense. Let me ask you this. When I put in the code behind like: int b = Convert.ToInt64(long)(TextBoxTL.Text);. I will not code it like that but let TryParse do that for me instead or is TryParse just checking to see if crap is entered and handles the crap for me to let the user know that they entered crap?
An int can store values between -2^32-1 to 2^32. To store anything larger, use long data type which can store between -2^64-1 to 2^64.
 
Share this answer
 
Comments
Computer Wiz99 9-Jul-14 11:11am    
Shameel, So I have this: int a = Convert.ToInt32(TextBoxTUNA.Text); and I changed it to this:
int b = Convert.ToInt64(long)(TextBoxTL.Text);. I get and error between (long)(TextBoxTL.Text). How can I fix this?
Andreas Gieriet 9-Jul-14 12:07pm    
Here again, you seem not to understand what number ranges and types mean. Do you know what value (2^32)-1 is? Compare it with your expected value range!
Andi
C#
int a = Convert.ToInt32(TextBoxTUNA.Text);

Problem in above code with TextBoxTUNA.Text value 2181003000 is
You can not store big no in small.
-2,147,483,648 .. 2,147,483,647 is the range of int datatype; so how can you store 2181003000 in int!!

Its like try to put 1.5 liter into 1 liter can.
You can do something like
C#
Int32 a = Convert.ToInt32(TextBoxTUNA.Text);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900