Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
objdal.Name1 = txt_name.Text;
objdal.Address= txt_address.Text;
objdal.Mob =Convert.ToInt32(txt_mob.Text);


i have this code in codebehind file.

cmd.Parameters.AddWithValue("@name1", Name1);
cmd.Parameters.AddWithValue("@address",Address);
cmd.Parameters.AddWithValue("@mob_no", Mob_no);

this code in DAL file.


here i have take mob no as int datatype.But error is comming like this...
System.OverflowException: Value was either too large or too small for an Int32.

what i have do wrong in my code?
Posted
Comments
Thomas Daniels 20-Jan-13 12:02pm    
What's the value of txt_mob.Text?

Hello

Int32 takes a value between 2,147,483,647 and -2,147,483,648

See here:
http://msdn.microsoft.com/en-us/library/system.int32.aspx[^]

If your value falls out of this range it will throw this exception while trying to convert it to int32.

Valery.
 
Share this answer
 
Comments
Sandeep Mewara 21-Jan-13 0:01am    
Yep. 5!
System.OverflowException: Value was either too large or too small for an Int32
As you said, you are using phone number for it, most probably it is too large from what an Int32 can handle.

For Int32, following is the range of value: 2,147,483,647, ( thus as a phone number: (214) 748-3647 ). Anything above that will throw the exception.

For storing a phone number, either use a Int64, bigint or long. Even string would do if that fits in for you.
 
Share this answer
 
Comments
Abhinav S 20-Jan-13 12:09pm    
5!
An int32 can only hold numbers that fit in 31 bits (the other bit is used to show if the value is positive or negative)
Which means that if you mobile number is larger than 2147483647 it won't fit in an integer. You could use a long which holds 64 bits, but I would store all phone numbers as strings so that they could potentially hold the International number indicate, "+"

EDIT: typo: "boys" for "bits" - stupid predictive text! - OriginalGriff
 
Share this answer
 
v2
Comments
Abhinav S 20-Jan-13 12:08pm    
5. Of course. Very similar to my answer. :)
An int32 lies between -2,147,483,648 to 2,147,483,647. Your mobile number is possibly larger than this.

Use string for mobile number. A mobile number can actually contain special character likes +, - etc.
 
Share this answer
 
Comments
Sandeep Mewara 21-Jan-13 0:00am    
Yep. 5!
Abhinav S 21-Jan-13 0:33am    
Thank you.
Change Mobile number field int to nvarchar(12) in database .
 
Share this answer
 
v2
Comments
[no name] 21-Jan-13 2:50am    
Why you Down vote my Answer.
[no name] 21-Jan-13 2:58am    
I don't know who has downvoted your answer but this is not the solution.
[no name] 21-Jan-13 3:02am    
ok
you can refer Solution 3
Int32 can't handle large mobile no value like '9867365362' because its size is small
if you are using .NET then you can use BigInteger.
To use BigInteger you have to add reference to assembly System.Numerics
 
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