简体   繁体   English

对于文本框中的Int32,值太大或太小

[英]Value was either too large or too small for an Int32 in Textbox

I am getting error on this int b = Convert.ToInt32(qty.Text); 我在这个int b = Convert.ToInt32(qty.Text);上得到错误int b = Convert.ToInt32(qty.Text); if i type more than 11 numbers in quantity textbox 如果我在数量文本框中键入超过11个数字

if (Item_Name.Text == dr["Item_Name"].ToString() && Item_Code.Text == dr["Item_Code"].ToString())
                {
                    int a = Convert.ToInt32(dr["Selling_Price"].ToString());
                    if (qty.Text == "")
                    {
                        Selling_Price.Text = "";
                    }
                    else
                    {
                        int b = Convert.ToInt32(qty.Text); //Error On This Line
                        int c = a * b;
                        Selling_Price.Text = c.ToString();
                    }
                }

You're getting that exception because an integer cannot store a number that large . 您正在获得该异常,因为整数不能存储大的数字

Int32.MaxValue Field : The value of this constant is 2147483647 Int32.MaxValue Field:此常量的值为2147483647

Switching to a long should solve the problem for you. 切换到long应该为您解决问题。 When you're dealing with numbers that represent money, you might want to consider using decimal too. 当您处理代表金钱的数字时,您可能也想考虑使用decimal

if (Item_Name.Text == dr["Item_Name"].ToString() && Item_Code.Text == dr["Item_Code"].ToString())
{
    int price = Convert.ToInt32(dr["Selling_Price"].ToString());
    if (qty.Text == "")
    {
        Selling_Price.Text = "";
    }
    else
    {
        long quantity = Convert.ToInt64(qty.Text);
        long total = price * quantity;
        Selling_Price.Text = total.ToString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM