Error System.OverflowException:对于Int32,值太大或太小

时间:2017-05-13 18:58:50

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

 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样式,NumberFormatInfo信息)处于System.Convert.ToInt32(String value)的Pages_register.Button3_Click(Object sender,EventArgs e)中的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; }
} 

此错误的任何解决方案

1 个答案:

答案 0 :(得分:1)

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

  

https://stackoverflow.com/a/23637154

但要避免此异常,您可以使用int.TryParse()

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