2012-02-22 48 views
0

這是代碼。字符串到Int轉換:Convert.toInt16

CODE 1: cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = Convert.ToInt32(ratecenterid.Text); 

CODE 2: cmd.Parameters.Add("@QuantityThreshold", OleDbType.Integer).Value = Convert.ToIn32(quantityThreshold.Text); 

我得到的CODE 2以下錯誤,但不是在CODE 1

錯誤:值是太大或太小,一個Int32。

好心幫我

問候, 阿瓊

+8

難道不是錯誤信息不言自明? – 2012-02-22 13:32:11

+2

向我們顯示'quantityThreshold.Text'的值。 – 2012-02-22 13:32:42

+0

@SaeedAmiri「QuantityThreshold = 4036953909」的樣本值爲 – 2012-02-22 13:35:04

回答

5

嘗試在quantityThreshold文本框中輸入一個更小或更大的價值。

更新

好了,看完您的評論迴應賽義德·阿米里,4036953909是一個int過大 - 它的範圍-21474836482147483647

你可以使用一個uint具有範圍04294967295

Convert.ToUInt32(ratecenterid.Text) 

雖然你將可能需要將參數類型更改爲OleDbType.UnsignedInt

+0

ratecenterid是一個主鍵。這是不可編輯的加上只讀通過標籤,而更新行動 – 2012-02-22 13:36:53

+0

對不起,我是'quantityThreshold'文本框;因爲那是你在 – 2012-02-22 13:40:37

4

從MSDN - Int32 Structure

Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32.MinValue constant) through positive 2,147,483,647 (which is represented by the Int32.MaxValue constant. The .NET Framework also includes an unsigned 32-bit integer value type, UInt32, which represents values that range from 0 to 4,294,967,295.

您要使用的值是不可能的代表作爲Int32

不知道爲什麼你的標題是關於Int16爲您的代碼和錯誤指示您正在使用Int32 - 無論 - Int16有一個更小的範圍比Int32

The Int16 value type represents signed integers with values ranging from negative 32768 through positive 32767.

您必須要麼使用UInt32long

+0

上得到的錯誤,如果它不夠,你有另一個OleDbType.Integer的問題,有相同的限制 – Steve 2012-02-22 13:41:13

2

Int32的範圍爲-2147483648到2147483647.您可以使用較大的類型,例如uint(0到4294967295)或長(-9.2233E + 18到9.2233E + 18)。

3

sample value for "QuantityThreshold = 4036953909"

對。那不會適合Int16(根據您的標題),其最大值爲32767.這甚至不適合Int32(根據您的代碼)(最大值2,147,483,647)。它符合UInt32,並將很容易適合Int64 - 也許後者是你真正想要的?

(您還應該澄清您的問題 - 您使用的是Int32還是Int16?)

1

你得到了,爲什麼在其他的答案,但我沒有看到任何人提ToInt64。只需將ToInt32更改爲ToInt64,並確保@QuantityThreshold的數據類型可以處理它。

The Int64 value type represents integers with values ranging from negative 9,223,372,036,854,775,808 through positive 9,223,372,036,854,775,807.

1

要解決您的問題,您將不得不將驗證放在文本框 - quantityThreshold上,它應該具有Int32範圍內的值。或者使用更大的類型。

2

您當前值UInt32範圍之內,並且OleDbType.Integer大小爲4字節,被映射到Int32,如果你想使用UInt32你應該使用UnsignedInt爲您的參數類型,並使用UInt32的轉換:

cmd.Parameters.Add("@QuantityThreshold", OleDbType.UnsignedInt).Value = 
    Convert.ToUIn32(quantityThreshold.Text)