2017-05-13 64 views
-3
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 \網頁\ register.aspx.cs:行38 我的模型類錯誤System.OverflowException:值被太大或太小,一個Int32

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; } 
} 

任何解決這個錯誤

+0

怎麼樣的錯誤你不明白? – TZHX

+1

我很抱歉,這裏有什麼問題?如果您嘗試將一個太小或太大的值存儲到「Int32」變量中,則會從「Convert.ToInt32」中得到該錯誤。爲什麼你會用int來存儲電話號碼? **使用一個字符串!**它不是一個數字值,它是一系列的數字。 –

+0

「Int32」的上限爲2,147,483,647。根據國際區號和本地格式,電話號碼可能具有更高的最大範圍,如999,999,999,999。是的,它會溢出。無論如何,你不應該把電話號碼存儲爲'int'。與郵政編碼一樣。如果你沒有對它進行數學運算,把它作爲一個字符串存儲。 – Abion47

回答

1

使用string類型來存儲的電話號碼和郵政編碼

https://stackoverflow.com/a/23637154

但要避免這個例外,你可以使用int.TryParse()

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