C# - 在出现错误 ASP.NET 保存配置文件'Object reference not set'



当我试图保存配置文件时,我得到的对象引用没有设置为对象错误的实例。

我试着添加一个"if"陈述if(this.Profile.ProfileDetails!=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();
    }

this.Profile.ProfileDetail可能不是引发异常的行。你进入密码了吗?它可能与.SelectedValue有关。如果没有选择任何值,它将抛出null。

最新更新