Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello friend,
 
I am using this code to count number
 
           int Startcount = Convert.ToInt32(TextBox1.Text);
           int Endcount = Convert.ToInt32(TextBox2.Text);
           int count = 0;
           for (int i = Startcount; i < Endcount; i++)
           {
               count = i;
           }
           label.Text = "Number: " + count.ToString();
There is 0 value in textbox1 and TextBox2 value is 100000000000 this
 
i am gating error when my TextBox2.text = 100000000000
 
i want to count number based on set in TextBox1 to TextBox2
please give me soluton how can i solve cont any nuber lent from TextBox1 to TextBox2
Thank you
Posted 10-Feb-13 0:49am
Edited 10-Feb-13 0:54am
v4
Comments
Alan N at 10-Feb-13 6:02am
   
When asking a question it is important that you include all relevant information.
In this case we need to know what exception you see and on which line on which it occurs.
bikramjeet.sm at 10-Feb-13 7:21am
   
this is a error with this current code Value was either too large or too small for an Int32. and when i use Convert.ToInt64 then i will get Error Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
Alan N at 10-Feb-13 8:35am
   
What I was trying to get you to say was something like
 
I am getting a System.OverflowException with the message "Value was either too large or too small for an Int32." on the line
 
int Endcount = Convert.ToInt32(TextBox2.Text);
 
That error happened at run time and OriginalGriff has explained why.
 
Now you have a compile time error as you have written something like
 
int Endcount = Convert.ToInt64(TextBox2.Text);
 
and the compiler knows that an Int64 will not fit into an int (Int32). The correct line would be
 
long Endcount = Convert.ToInt64(TextBox2.Text);
 
Always, always post your code along with the exact error message and the line where it occurs. Otherwise the people who want to help you are guessing.
bikramjeet.sm at 10-Feb-13 13:02pm
   
yes it is

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The largest number you can hold in an Int32 is:
  2,147,483,647
So your value of:
100,000,000,000
is way, way too big!
 
Change your Convert.ToInt32 to Convert.ToInt64 calls instead, and use long values instead of int.
  Permalink  
Comments
bikramjeet.sm at 10-Feb-13 7:35am
   
this is a error with this current code Value was either too large or too small for an Int32. and when i use Convert.ToInt64 then i will get Error Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
OriginalGriff at 10-Feb-13 7:41am
   
That is why I said "and use long values instead of int."
 
An int is an Int32, a long is an Int64. You cannot fit any value greater than 2,147,483,647 in any Int32.
bikramjeet.sm at 10-Feb-13 7:58am
   
i am doing this same what you asked but still error
OriginalGriff at 10-Feb-13 8:13am
   
I bet you aren't! :laugh:
If you are getting the error message "Value was either too large or too small for an Int32" then you are still trying to use an int or Int32 value - you can't put that big a number in it.
Try and copy'n'paste your code fragment here - I'll see if I can spot what you have missed.
bikramjeet.sm at 10-Feb-13 8:32am
   
What did i have missed in it
OriginalGriff at 10-Feb-13 9:28am
   
Somewhere, you are assigning the long value to an int - I can't tell you where, because I don't have access top your code...:laugh:
bikramjeet.sm at 10-Feb-13 9:30am
   
textbox2
OriginalGriff at 10-Feb-13 9:40am
   
No, you can't assign an int or a long to a TextBox - you can to some properties, but not to the object itself.
Show the code you are using - it'll be a lot quicker!

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 BillWoodruff 220
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,817
2 DamithSL 5,461
3 Manas Bhardwaj 4,946
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100