2013-07-17 27 views
0

當我嘗試保存配置文件時,我得到的對象引用未設置爲對象錯誤的實例。C# - 保存配置文件ASP.NET給出'對象引用未設置'錯誤

我嘗試添加一個「如果」 聲明 如果(this.Profile.ProfileDetail!= NULL) 但它仍然給了我同樣的錯誤。

這是我的代碼:

private void SaveProfile() 
    { 


      this.Profile.ProfileDetail.FullName = txtfname.Text; 
      this.Profile.ProfileDetail.IdentityCardNo = txtcardno.Text; 
      this.Profile.ProfileDetail.IdentityCardColour = ddlcardcolor.SelectedValue; 
      this.Profile.ProfileDetail.IdentityCardExpiryDate = DateTime.Parse(txtcardexp.Text); 
      this.Profile.ProfileDetail.Race = ddlrace.SelectedValue; 
      this.Profile.ProfileDetail.Religion = ddlreligion.SelectedValue; 
      this.Profile.ProfileDetail.ContactInformation.Address1 = txtaddress1.Text; 
      this.Profile.ProfileDetail.ContactInformation.Address2 = txtaddress2.Text; 
      this.Profile.ProfileDetail.ContactInformation.Address3 = txtaddress3.Text; 
      this.Profile.ProfileDetail.ContactInformation.Postcode = txtpostcode.Text; 
      this.Profile.ProfileDetail.ContactInformation.District = ddldistrict.SelectedValue; 
      this.Profile.ProfileDetail.ContactInformation.OfficePhone = txtphoneoffice.Text; 
      this.Profile.ProfileDetail.ContactInformation.HomePhone = txtphonehome.Text; 
      this.Profile.ProfileDetail.ContactInformation.MobilePhone = txtphonecell.Text; 
      this.Profile.ProfileDetail.ContactInformation.Email = txtemail.Text; 




     //this.Profile.ProfileDetail.FirstName = txtFirstName.Text; 
     //this.Profile.ProfileDetail.LastName = txtLastName.Text; 
     //this.Profile.ProfileDetail.IdentityCardNo = txtICNo.Text; 
     //this.Profile.ProfileDetail.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text); 
     //this.Profile.ProfileDetail.Gender = (Gender)Enum.Parse(typeof(Gender), ddlGender.SelectedValue, true); 
     //this.Profile.Preferences.TimeZone = ddlTimeZones.SelectedValue; 
     //this.Profile.Preferences.Culture = ddlCultures.SelectedValue; 

     this.Profile.Save(); 

     this.ResetElements(); 
    } 
+1

'this.Profile'可能爲空,在這種情況下'this.Profile'是什麼?你是登錄到應用程序還是你的班級的這個屬性?或當前上下文的配置文件? – Damith

+0

@Damith個人資料爲空。我點擊編輯填寫信息,但我無法保存。 – Belzelga

+0

@Damith謝謝。它現在工作正常。我添加了如果這個配置文件在東西之上是空的。謝謝:'D – Belzelga

回答

0

this.Profile.ProfileDetail可能不是一個拋出異常。你是否加入了代碼?它可能與.SelectedValue一致。如果沒有選擇任何值,它將拋出null。

+0

我嘗試評論4條具有「選定值」的行,但它仍然不起作用:( 現在我在address1.text處出現錯誤 – Belzelga

相關問題