Search code examples
c#asp.netprofile

ASP.NET Profile not updating certiain values? Probably a code issue


Alright, so I've been trying to make an ASP.NET website that uses basic profiling features, and so far I'm running into trouble with the code I have to display and edit profile information.

Basically, in the top part of my page, it shows your "Screenname" and "Bio" - both defined in the same way in the web.config (below). In my page, I have two textboxes and a save button, IDed as AssignScreenname, AssignBio, and ChangeProfileSaveButton. When the save button is clicked, an if & else if each run to check if either box is null or an empty string, and if one isn't that one's value is assigned to the profile. Then, the code calls Profile.save() and redirects you back to the same page to insure a complete refresh of the entire page.

My problem is that when you assign one value, the other value gets scratched out. For example, assigning a Bio value means that any pre-existing Screenname value gets erased. I don't understand why it does this, because it should only be assigning the boxes to the profile is there is no text in them.

I also found it interesting that removing Profile.Save() did nothing, so maybe the problem has something to do with that?

Codes: The element of my Web.Config:

<profile>
    <properties>
      <add name="Username"/>
      <add name="Screenname" />
      <add name="Bio" />
      <add name="ProfilePictureURL" />
    </properties>
  </profile>

My C# that assigns the values:

 protected void ChangeProfileSaveButton_Click(object sender, EventArgs e)
 {
    if (Page.IsValid)
    {
        if (AssignScreenName.Text != "" || AssignScreenName.Text != null)
        {
            Profile.Screenname = AssignScreenName.Text;
        }
        if (AssignBio.Text != "" || AssignBio.Text != null)
        {
            Profile.Bio = AssignBio.Text;
        }
    }
    Profile.Save();
    Response.Redirect("MyProfile.aspx");
    }
}

I'll try to check back here daily to answer comments, so if you need anything feel free to ask! All ideas are welcome, and thanks!


Solution

  • Change the System.Web section of your Web.Config file to set automaticSaveEnabled = false;

    ex.

    <profile enabled="true" automaticSaveEnabled="false">
    

    Cleaner code:

    if (Page.IsValid)
    {
        if (!String.IsNullOrEmpty(AssignScreenName.Text) Profile.Screenname = AssignScreenName.Text;
        if (!String.IsNullOrEmpty(AssignBio.Text) Profile.Bio = AssignBio.Text;        
    }
    Profile.Save();
    Response.Redirect("MyProfile.aspx");
    

    Also you can use:

    Profile.SetPropertyValue("propertyname",newvalue);
    Profile.Save();
    

    example:

    Profile.SetPropertyValue("Screenname",assignscreenname.Text);
    Profile.Save();
    

    for each property you'd like to set.