Page view counter

The name 'Profile' does not exist in the current context

Last post 11-29-2008 9:29 PM by umbyersw. 11 replies.

Sort Posts:

  • The name 'Profile' does not exist in the current context

    03-30-2006, 6:19 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    Basicly, I'm trying to create a custom base page and inherit it to all of my aspx pages. I'm getting the above error:

    The name 'Profile' does not exist in the current context

     

    The custom base page looks like this:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.Profile;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Summary description for ManagePage
    /// </summary>

        public class ManagePage : System.Web.UI.Page
        {
            protected void Page_PreInit(Object sender, EventArgs e)
            {
                //define skin.
                MasterPageFile = Profile.Master;           
            }
        }

     And an example of an aspx page, looks like this:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;

    public partial class home : ManagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            KFD.DataLayer dl = new KFD.DataLayer();
            litInfoUpdates.Text = dl.GetInfoUpdates();
        }
    }

     I'm new to using aspx like this so I'm not sure if the Profile object is getting built after the base class tries to evaluate, or if I'm making a simple mistake some where. Any assistance would be appreciated.
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 2:36 AM
    Answer
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 5:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs
    are you looking for:

    HttpContext.Current.Profile

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 12:54 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40
    I believe so, yes. Should I explictly sate it as such?
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 1:07 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    http://msdn2.microsoft.com/en-us/library/2y3fs9xs(VS.80).aspx

     

    this is what I'm refering to. which I believe is HttpContext.Profile

  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 1:08 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 5:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs
    that is how I reference Cache in my app.
    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 1:09 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    I did explictly state it, and it's no longer out of context. However, I can not access the custom varaibles associated with the current profile.

    It's like the actually profile object isn't being built yet.

  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 3:17 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 5:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs
    are you trying to get the membership info?

    Here is some sample code that works for me in my page_unload event.

     protected void Page_UnLoad(object sender, EventArgs e)
        {
            string urlReferrer = string.Empty;
            string userAgent = string.Empty;
            string userHostName = string.Empty;
            string userHostAddress = string.Empty;
            string userPrimaryLanguage = string.Empty;
            string sessionId = string.Empty;
            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                urlReferrer = HttpContext.Current.Request.UrlReferrer == null ? string.Empty : HttpContext.Current.Request.UrlReferrer.ToString();
                userAgent = HttpContext.Current.Request.UserAgent == null ? string.Empty : HttpContext.Current.Request.UserAgent.ToString();
                userHostName = HttpContext.Current.Request.UserHostName == null ? string.Empty : HttpContext.Current.Request.UserHostName.ToString();
                userHostAddress = HttpContext.Current.Request.UserHostAddress == null ? string.Empty : HttpContext.Current.Request.UserHostAddress.ToString();
                userPrimaryLanguage = HttpContext.Current.Request.UserLanguages == null ? string.Empty : HttpContext.Current.Request.UserLanguages[0].ToString();
                sessionId = Session.SessionID.ToString();
            }

            string userName = string.Empty;
            if (IsPostBack)
            {
                userName = "PostBack";
            }
            else
            {
                userName = string.Empty;
            }

            utils.LogPageLoadTime(startTime, "Default.aspx", userName,
                urlReferrer, userAgent, userHostName, userHostAddress, userPrimaryLanguage,sessionId);

        }


    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 3:36 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    It's nearly the same as membership. What I've discovered, however, is you can't access it from a custom base class. It has to be used on the actuall aspx page. It all has to do with when the user gets athunticated (I'm using integrated auth). If the user hasn't been athunticated it can't find the Profile for that user yet.

    I tried a work around, all be it a little odd, but I didn't like the results. You can create an new aspx page remove all the code from the .aspx except for the necessary header and apply the code you want to happen on every page to the codebehind. Then in the code behind of all the other aspx pages inherit the codebehind you just created. This will let you use the Profile object (created by ProfileCommon after athuntication takes place) in a "base page" you can inherit.

    The down side is you have to remove the reference to the MasterPageFile from your aspx pages or else it will overwrite what your inheriting from the "base page" you just created. This, of course, causes errors during build and is quite annoying.

    I just wish there was a way to get this to work from a base class normally but I haven't been able to figure it out yet.

  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 5:54 PM
    Answer
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    http://www.odetocode.com/Articles/440.aspx

    The above site answers ALL the questions I had in regards to Profiles. In fact it is a very handy article if you ever intend on using Profiles I suggest you read it.

    The solution to my issue basicly looks like this:

    using System;
    using System.Web;
    using System.Web.UI;

    /// <summary>
    /// Summary description for ManagePage
    /// </summary>


    public class ManagePage : Page
    {
        public ManagePage()
        {
            PreInit += new EventHandler(ManagePage_PreInit);
        }

        void ManagePage_PreInit(object sender, EventArgs e)
        {
            ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
            profile.Master = "/skin/Basic/default.master";
            MasterPageFile = profile.Master;
        }
    }

     
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 5:54 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 5:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs
    I have to admit I always use forms authentication, but that being said, here is an example of using Profile in the updatemethod of the ObjectDataSource.  (You can generate your own on my web site at http://painfreeods.peterkellner.net

     [DataObjectMethod(DataObjectMethodType.Update, true)]
    public void Update(string userName, string email,bool isLockedOut,
    bool isApproved, string comment, DateTime lastActivityDate, DateTime lastLoginDate
    ,string firstName,string lastName,bool advancedMode,string address_Street,string address_City,string address_State,string address_Zip
    )
    {
    bool dirtyFlagMu = false;

    MembershipUser mu = Membership.GetUser(userName);

    ProfileCommon pc = (ProfileCommon)ProfileBase.Create(mu.UserName, true);
    pc.FirstName = firstName;
    pc.LastName = lastName;
    pc.AdvancedMode = advancedMode;
    pc.Address.Street = address_Street;
    pc.Address.City = address_City;
    pc.Address.State = address_State;
    pc.Address.Zip = address_Zip;
    pc.Save();

    if (mu.IsLockedOut && !isLockedOut)
    {
    mu.UnlockUser();
    }

    if ( string.IsNullOrEmpty(mu.Comment) || mu.Comment.CompareTo(comment) != 0)
    {
    dirtyFlagMu = true;
    mu.Comment = comment;
    }

    if (string.IsNullOrEmpty(mu.Email) || mu.Email.CompareTo(email) != 0)
    {
    dirtyFlagMu = true;
    mu.Email = email;
    }

    if (mu.IsApproved != isApproved)
    {
    dirtyFlagMu = true;
    mu.IsApproved = isApproved;
    }

    if (dirtyFlagMu == true)
    {
    Membership.UpdateUser(mu);
    }
    }



    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: The name 'Profile' does not exist in the current context

    03-31-2006, 5:58 PM
    • Loading...
    • triadfate
    • Joined on 03-30-2006, 11:13 PM
    • Posts 8
    • Points 40

    haha, yeah, my response hasn't posted yet but I just saw your response. I basicly did the same thing, as you should see in the code. My major issue was I didn't know enough about the Profile object, how and when it was getting generated.

    But, I have it under control now. Thank you very much for your time in this. It was starting to drive me mad, lol.

  • Re: The name 'Profile' does not exist in the current context

    11-29-2008, 9:29 PM
    • Loading...
    • umbyersw
    • Joined on 10-26-2007, 6:08 PM
    • Edmonton, AB, Canada
    • Posts 41
    • Points 209

    triadfate:

    I did explictly state it, and it's no longer out of context. However, I can not access the custom varaibles associated with the current profile.

    It's like the actually profile object isn't being built yet.

    In a web application project (as opposed to a web site project), the Profile class is not auto-generated from config.

    For those unfortunate souls, you will need to use the two following techniques to manage your profile settings.

    // Retrieving values
    string MyValue = (string) Context.Profile.GetPropertyValue(
    "MyValue");

    // Setting values
    if (Context.Profile.UserName != null) {
      Context.Profile.SetPropertyValue("MyValue", "something");
      Context.Profile.Save();
    }

    Wes
    www.umbyersw.com
Page 1 of 1 (12 items)