3

..been browsing the net but no luck.. I need to use the ProfileCommon but I can't reference any assemblies to use it.. can someone help?

user384080
  • 4,576
  • 15
  • 64
  • 96

2 Answers2

4

When you have an ASP.NET web site, not application project, and make use of Profile the ProfileCommon file gets autogenerated in the temporary ASP.NET files. When you're using an ASP.NET project however you'll need to create that on your own. Take a look at this sample on how to implement it on your own. The sample is for usage in an MVC application project but since that's based on ASP.NET itself the concepts remain the same.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
  • i'm not using MVC and I have checked the class was generated in the ASP.Net temporary folder. anyway.. I have a project (developed by previous programmer) and within the project it has a line ProfileBase pb = ProfileCommon.GetUser(....);. It is currently working fine.. but I need to create a new project that use the ProfileCommon as well but I cannot find a way to reference it.. please help! – user384080 Aug 26 '10 at 08:08
  • You don't need to reference that file. If you're in a new website and alter the web.config to make use of Profile then it gets autogenerated by default. For a web application you need to craft it yourself. – Kris van der Mast Aug 26 '10 at 08:36
  • what do you mean by "craft it"? – user384080 Aug 26 '10 at 22:38
  • @ronald: should've been create. Either way, did you already try this solution? – Kris van der Mast Aug 27 '10 at 05:05
  • Did you try out the sample in the link I provided? You simply need to create a new class, add the needed code for the ProfileCommon in it with the correct properties and set it accordingly in the web.config file. – Kris van der Mast Aug 29 '10 at 14:16
  • I need to create the profile if it does not exist. The ProfileBase.Create will not create the new one! – user384080 Oct 18 '10 at 00:44
1

This is a dynamic type generated by the framework. On runtime the type 'ProfileCommon' exists.

If you can use the C# 4.0 language features you can wrap the behavior of ProfileCommon in a dynamic object.

I have the following extended properties

<profile enabled="true" >
      <properties>
        <add name="FirstName" type="String" />
        <add name="LastName" type="String" />
      </properties>
</profile>

The code example to use the dynamic object is

dynamic profile = new ProfileData();
var name = profile.FirstName + ' ' + profile.LastName;

The implementation of ProfileData:

public class ProfileData: DynamicObject
    {
        private readonly ProfileBase profileBase;

        /// <summary>
        /// Profile Data for the current user.
        /// </summary>
        public ProfileData()
        {
            profileBase = HttpContext.Current.Profile;
        }

        /// <summary>
        /// Profile data for user with name <paramref name="userName"/>
        /// </summary>
        /// <param name="userName"></param>
        public ProfileData(string userName)
        {
            profileBase = ProfileBase.Create(userName);         
        }

        // If you try to get a value of a property 
        // not defined in the class, this method is called.
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            try
            {
                result = profileBase.GetPropertyValue(binder.Name);
            }
            catch(SettingsPropertyNotFoundException)
            {
                result = null;
                return false;
            }
            return true;            
        }

        // If you try to set a value of a property that is
        // not defined in the class, this method is called.
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            try
            {
                profileBase.SetPropertyValue(binder.Name, value);
                return true;
            }
            catch (SettingsPropertyNotFoundException)
            {               
                return false;
            }           
        }

        /// <summary>
        /// Persist the profile data.
        /// </summary>
        public void Save()
        {
            profileBase.Save();
        }
    }
Sentient
  • 2,185
  • 2
  • 19
  • 20