3

I can't seem to find why or where but LastActivityDate is always updated even when i set Membership.GetUser(userName, false) to false so it won't update the LastActivityDate. I dopn't have any other code anywhere else that accesses the GetUser aswell. So I created a property in the profile called LastActivity this works sort of but i don't understand why GetUser is always updating LastActivityDate everytime i refresh the page.

    private void SwitchMode(Mode mode)
{
    ProfileCommon profile = Profile.GetProfile(UserName);
    MembershipUser mu = Membership.GetUser(UserName, false);

    bool isOnline = minutes(DateTime.Now - profile.LastActivity) >= Membership.UserIsOnlineTimeWindow ? false : true;

    lblActivity.Text = mu.LastActivityDate.ToString();
    switch (mode)
    {
        case global::Mode.Readonly:
            profileAvatar.Visible = true;
            profileDetails.Visible = true;
            profileForm.Visible = false;

            lblUserName.Text = UserName;

            if (profile.Website != null)
            {
                lblWebsite.Visible = false;
                lnkWebsite.Visible = true;
            }
            else
            {
                lblWebsite.Visible = true;
                lnkWebsite.Visible = false;
            }

            lblSummary.Text = profile.Summary;
            lnkWebsite.Text = profile.Website == string.Empty ? "-" : profile.Website;
            lnkWebsite.NavigateUrl = profile.Website == string.Empty ? "-" : profile.Website;
            lblLocation.Text = profile.Address.StateRegion + ", " + profile.Address.Country;
            lblFullname.Text = profile.FirstName + " " + profile.LastName;
            lblEmail.Text = profile.ShareEmail && mu != null ? mu.Email : "-";
            lblGender.Text = profile.Gender == 0 ? "Male" : profile.Gender == 1 ? "Male" : "Female" ;
            lblAge.Text = Convert.ToString(Math.Floor(DateTime.Today.Subtract(profile.BirthDate).TotalDays / 365.25));

            lblMemberfor.Text = PrintTimeSpan1(DateTime.Now - mu.CreationDate);
            lblVisited.Text = profile.Visits.ToString();
            lblSeen.Text = PrintTimeSpan1(DateTime.Now - profile.LastActivity) + " ago";
            lblProfileViews.Text = profile.Views.ToString();

            if (HttpContext.Current.User.Identity.Name != UserName)
            {
                profile.Views += 1;
                profile.Save();                    
            }


            if (isOnline)
            {
                lblStatus.Text = "online";
            }
            else
            {
                lblStatus.Text = "offline";
            }


            break;
        case global::Mode.Edit:
            profileAvatar.Visible = true;
            profileDetails.Visible = false;
            profileForm.Visible = true;
            lblUserName.Text = UserName;

            txtFirstName.Text = profile.FirstName;
            txtLastName.Text = profile.LastName;
            radioGender.SelectedIndex = profile.Gender;
            ddlDay.SelectedValue = profile.BirthDate.Day.ToString();
            ddlMonth.SelectedValue = profile.BirthDate.Month.ToString();
            ddlYear.SelectedValue = profile.BirthDate.Year.ToString();
            txtWebsite.Text = profile.Website;
            ddlOccupation.SelectedValue = (profile.Occupation == string.Empty ? "0" : profile.Occupation);
            txtStreet.Text = profile.Address.Street;
            txtZipCode.Text = profile.Address.ZipCode;
            txtStateRegion.Text = profile.Address.StateRegion;
            ddlCountry.SelectedValue = (profile.Address.Country == string.Empty ? "0" : profile.Address.Country);
            txtAvatar.Text = profile.Forum.AvatarUrl;
            txtForumSignature.Text = profile.Forum.Signature;
            chkNewsLetter.Checked = profile.NewsLetter;
            chkShareEmail.Checked = profile.ShareEmail;
            txtSummary.Text = profile.Summary;

            break;
        case global::Mode.Insert:
            profileAvatar.Visible = false;
            profileDetails.Visible = false;
            profileForm.Visible = true;
            break;
    }
}
ONYX
  • 5,679
  • 15
  • 83
  • 146

2 Answers2

3

Your call to Profile.GetProfile(UserName) is updating the date. I had the same problem. See my question here: Is it possible to access a profile without updating LastActivityDate? Basically, you'll have to modify the stored procedure that is getting the profile details and remove a small block of code.

Community
  • 1
  • 1
WildJoe
  • 5,740
  • 3
  • 26
  • 30
0

If you take a look at: http://msdn.microsoft.com/en-us/library/system.web.profile.profilebase.lastactivitydate.aspx it says that the last activity date gets updated when the profile is either read or modified. In your code you "read" the profile (profile.Views += 1), so that may be what's doing it, not the call to Membership.GetUser. I haven't tested this myself, but hopefully it helps...

joshuahealy
  • 3,529
  • 22
  • 29
  • If thats the case what can I do to change/avoid the profile.Save in this situation – ONYX Mar 08 '12 at 00:32
  • The docs say "This property value is only modified by the default profile provider. Changes made to profile properties by a provider other than the default profile provider do not affect the value of the LastActivityDate property." So if you made your own provider I guess you can avoid the update. I've never done this myself but have a look at [Implementing a Profile Provider](http://msdn.microsoft.com/en-us/library/0580x1f5.aspx). This could be a lot of work though... – joshuahealy Mar 08 '12 at 00:56
  • Your other option is to add your own "activity date" property on the profile and update it manually yourself – joshuahealy Mar 08 '12 at 00:56
  • Yeah i added my own LastActivity then used userIsOnlineTimeWindow to get the timeout of the user updated my code – ONYX Mar 08 '12 at 00:59