如何解决Int32的值太大或太小。 [英] How to solve Value was either too large or too small for an Int32.

查看:1298
本文介绍了如何解决Int32的值太大或太小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Button1_Click1(对象发​​件人,EventArgs e)
{
captcha1.ValidateCaptcha(TextBox1.Text.Trim());
if (captcha1.UserValidated)
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = 您已输入有效的Captcha字符;
}
else
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = 您已输入InValid验证字符,请再次输入;
}
string a,c,d;
int b,f;
会话[ user] = TextBox1.Text;
a = TextBox1.Text;
b = Convert.ToInt32(TextBox2.Text);
c = TextBox3.Text;
d = TextBox4.Text;
f = Convert.ToInt32(TextBox6.Text);
cmd.CommandText = 插入SignUp值(' + a + ',' + d + ',' + c + ',' + b + ',' + f + ',1000000);
int r = cmd.ExecuteNonQuery();
if (r > 0
{
Response.Redirect( homepage.aspx) ;
}
其他 {

}
}

i使用转换.ToInt64()也是但同样的错误显示
b = Convert.ToInt32(TextBox2.Text); // 错误插入10位数的电话号码在此行中。

解决方案

您已超出整数的最大值 - 请参阅此参考 [ ^ ]



编辑 - 评论后的例子



使用 long 而不是 int



使用带有TryParse的long ...

  long  lVal; 
if long .TryParse(textBox1.Text, out lVal))
Console.WriteLine( 值为long。值{ 0},lVal);
else
Console.WriteLine( 值无法解析为长);





使用转换(我不推荐)

 Console.WriteLine( 使用转换{0},Convert.ToInt   64  (textBox1.Text)); 

将电话号码存储为字符串,而不是数字,因为它们可能具有前导零。


最好不要使用屏蔽文本框中的某些内容。有许多可用的解决方案:

Mask TextBox ASP。 NET控件

https://github.com/digitalBush/jquery.maskedinput

https://plugins.jquery.com/maskedinput [ ^ ]。



使用验证总是不好的您可以简单地强制执行始终有效的输入的情况。如果它不起作用(验证规则太复杂而无法清楚地执行它们),请使用顶部验证。



-SA

protected void Button1_Click1(object sender, EventArgs e)
        {
            captcha1.ValidateCaptcha(TextBox1.Text.Trim());
            if (captcha1.UserValidated)
            {
                Label1.ForeColor = System.Drawing.Color.Green;
                Label1.Text = "You have Entered Valid Captcha Characters";
            }
            else
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "You have Entered InValid Captcha Characters please Enter again";
            }
            string a, c, d;
            int b,f;
             Session["user"] = TextBox1.Text;
            a = TextBox1.Text;
            b = Convert.ToInt32(TextBox2.Text);
            c = TextBox3.Text;
            d = TextBox4.Text;
            f = Convert.ToInt32(TextBox6.Text);
cmd.CommandText="Insert into SignUp values('"+a+"','"+d+"','"+c+"','"+b+"','"+f+"',1000000)";
int r=cmd.ExecuteNonQuery();
if (r > 0)
{
    Response.Redirect("homepage.aspx");
}
else {

}
        }

i have used Convert.ToInt64() also but same error show
  b = Convert.ToInt32(TextBox2.Text);//Error are in this line, when insert 10 digit phone number.

解决方案

You have exceeded the maximum value for an integer - see this reference[^]

Edit - examples after comment

Use a long instead of int

Using a long with TryParse...

long lVal;
if (long.TryParse(textBox1.Text, out lVal))
    Console.WriteLine("Value is a long. Value {0}", lVal);
else
    Console.WriteLine("Value cannot be parsed as a long");



Using Convert (which I do not recommend)

Console.WriteLine("Using Convert {0}", Convert.ToInt64(textBox1.Text));


Store phone numbers as string, not numeric, as they might have leading zeros.


Instead of doing all that, you should better use something line a masked text box. There is a number of available solutions:
Mask TextBox ASP.NET Control,
https://github.com/digitalBush/jquery.maskedinput,
https://plugins.jquery.com/maskedinput[^].

It's always bad to use validation in cases when you can simply enforce input which is always valid. If it cannot work (validation rules are too complex to clearly enforce them), use the validation of top of that.

—SA


这篇关于如何解决Int32的值太大或太小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆