6

I am trying to add Mobile Number to my column which is of 10 digit

But it is giving me error as

Value was either too large or too small for an Int32

Here is the code

drpartyInfo[0]["MOB_NUM"] = string.IsNullOrWhiteSpace(e.Record["MOB_NUM"].ToString())
    ? DBNull.Value : (object)Convert.ToInt32(e.Record["MOB_NUM"].ToString());
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Nad
  • 4,605
  • 11
  • 71
  • 160
  • Just to be a bit clearer in respect to all answers given here. If you have and know the upper/lower bounds of the values, you should choose the smallest binary datatype that fits (`short`, `ushort`, `int`, `uint`, `long`, `ulong`). Databases *LOVE* those types, they occupy a fixed amount of bytes and can be ordered/indexed easier. A `string` (`varchar`) is not as easy to handle, so as a rule of thumb avoid `string` if you can. On the other hand if you have *less than ~10 million rows* in a table the difference will be barely noticeable. This is just a side note and not meant to be an answer. – pid Jan 24 '17 at 07:23
  • @pid: so according to you which datatype will be suitable here for mobile number? – Nad Jan 24 '17 at 07:26
  • 10 digits means the lower bound is 0 and the upper bound is 9,999,999,999. That fits into a `long` (`Int64`). Whatever number `n` fits into `log2(n) + 1` bits. Up that number to 8, 16, 32, 64 and you have the least type you require. – pid Jan 24 '17 at 07:29
  • You don't want to store phone numbers as numbers unless you store the prefix (area, country code) separately. – CodeCaster Jan 24 '17 at 07:40
  • WAIT! If `MOB_NUM` is a phone number don't use an integer at all. It really is a *code* which must be stored in a string and not a *quantity*! My bad, where I live we don't say *mobile number* but simply *phone number*. Leading `0` cannot be stored in an integer of any sort! USE A STRING!!! – pid Jan 24 '17 at 10:00
  • The only correct answer is **Ikram Turgunbaev**'s – pid Jan 24 '17 at 10:02
  • what's wrong about my answer @pid I ill appreciate the correction – MrVoid Jan 24 '17 at 13:36
  • @MrVoid A phone number may have leading zeroes such as 00927849, which, when converted to any of the integer types becomes 927849. To no avail you can re-convert it to a string (or render it on a web page) and get the leading zeroes back. – pid Jan 24 '17 at 21:24
  • very true, those digits will be lost, thanks – MrVoid Jan 25 '17 at 02:28

4 Answers4

4

Better you should use long, string or byte array.

You can refer this.

short.MaxValue:  32767
short.MinValue: -32768
ushort.MaxValue: 65535
ushort.MinValue: 0
int.MaxValue:    2,147,483,647
int.MinValue:   -2,147,483,648
uint.MaxValue:   4,294,967,295
uint.MinValue:   0
long.MaxValue:   9,223,372,036,854,775,807
long.MinValue:  -9,223,372,036,854,775,808
ulong.MaxValue:  18,446,744,073,709,551,615

You can use unsigned datatype, if you are sure that it will always be positive number.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
2

Mobile number can not be stored on int, change your type to string

kgzdev
  • 2,770
  • 2
  • 18
  • 35
2

If your number is bigger than 2147483647, it can't be stored into a int32, use string or use a int64, I doubt your number is bigger than 9223372036854775807

MrVoid
  • 709
  • 5
  • 19
1

Int32.MaxValue is 2,147,483,647.

If the mobile number is 10 digits long, there is a 79% chance it won't fit in an Int32. Try and Int64 (or UInt64) instead.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62