【问题标题】:Error System.OverflowException: Value was either too large or too small for an Int32 [closed]错误 System.OverflowException:对于 Int32,值太大或太小 [关闭]
【发布时间】:2017-05-14 03:35:55
【问题描述】:
 protected void Button3_Click(object sender, EventArgs e)
    {
    UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
    userStore.Context.Database.Connection.ConnectionString =
       System.Configuration.ConfigurationManager.ConnectionStrings
     ["db1ConnectionString"].ConnectionString;

    UserManager<IdentityUser> manager = new UserManager<IdentityUser>
    (userStore);
    //create new user and try to store in db
    IdentityUser user = new IdentityUser();
    user.UserName = txtUserName.Text;
    user.Email = txtEmail.Text;
    user.PhoneNumber = txtPhNo.Text;    

    if (txtPassword.Text == txtConfirmPassword.Text)
    {
        try
        {
            //create user object.
            //DB will be created /expanded automatically.
            IdentityResult result = manager.Create(user, txtPassword.Text);
            if (result.Succeeded)
            {
                **UserInformation1 info = new UserInformation1
                {
                    Address = txtAddress.Text,
                    FirstName = txtFirstName.Text,
                    LastName = txtLastName.Text,
                    PostalCode = Convert.ToInt32(txtPostalCode.Text),
                    PhoneNo = Convert.ToInt32(txtPhNo.Text),

                    Email = user.Email,
                    GUID = user.Id
                };
                UserInfoModel model = new UserInfoModel();
                model.InsertUserInformation(info);**
                //store user in db
                var authenticationManager = 
                HttpContext.Current.GetOwinContext().Authentication;

                //set to log in new user by cookie
                var userIdentity = manager.CreateIdentity(user, 
                DefaultAuthenticationTypes.ApplicationCookie);
                //log in the new user and redirect to homepage
                authenticationManager.SignIn(new 
 Microsoft.Owin.Security.AuthenticationProperties(), userIdentity);
                Response.Redirect("~/Pages/greetings_home.aspx");



            }
            else
            {
                litStatus.Text = result.Errors.FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            litStatus.Text = ex.ToString();
        }

    }
    else
    {
        litStatus.Text = "Password must match";
    }
    }

错误: System.OverflowException:对于 Int32,值太大或太小。在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Convert.ToInt32(String value) at Pages_register.Button3_Click(Object sender, EventArgs e) in c:\Users\shreya\Documents\Visual Studio 2015 \Project_Greetings\Pages\register.aspx.cs:第 38 行 我的模型课

public partial class UserInformation1
{
public int Id { get; set; }
public string GUID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public Int32 PostalCode { get; set; }
public Int32 PhoneNo { get; set; }
public string Email { get; set; }
} 

此错误的任何解决方案

【问题讨论】:

  • 错误不明白怎么办?
  • 很抱歉,这里有什么问题?如果您尝试将过小或过大的值存储到Int32 变量中,您将从Convert.ToInt32 收到该错误。为什么你会使用 int 来存储电话号码? 使用字符串! 这不是数值,而是一系列数字。
  • Int32 的上限为 2,147,483,647。根据国际区号和本地格式,电话号码可能具有更高的最大范围,例如 999,999,999,999。是的,会溢出来的。无论如何,您不应该将电话号码存储为int。与邮政编码相同。如果您不对其进行数学运算,请将其存储为字符串。
  • 和邮政编码???

标签: c# asp.net asp.net-web-api


【解决方案1】:

使用string类型存储电话号码和邮政编码

https://stackoverflow.com/a/23637154

但是为了避免这个异常,你可以使用int.TryParse():

int result;
if(!int.TryParse(txtPostalCode.Text, out result))
{
    // process wrong input
}

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 2013-12-18
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多