Programmatic Profile properties AND groups
First off, thank you to Geoff for pointing me to Fredrik's excellent post on programmatically setting Profile properties. All is well so far, but I'd like to use groups. It appears, however, that it's not as straight forward. I looked at the generated code to see how it's done, but the same code does not apply.
Here's what I have so far..
<profile enabled="true" inherits="PopForums.Profile.PopForumsProfile" defaultProvider="PopForumsProfileProvider">
<providers>
<add name="PopForumsProfileProvider" type="PopForums.Profile.PopForumsProfileProvider"/>
</providers>
</profile>
public class PopForumsProfile : ProfileBase
{
private PopForumsProfileGroup _popForums;
public virtual PopForumsProfileGroup PopForums
{
//get { return ((PopForumsProfileGroup)(this.GetProfileGroup("PopForums"))); }
get
{
if (_popForums == null) {
_popForums = new PopForumsProfileGroup();
_popForums.Init(this, "PopForums");
}
return _popForums;
}
}
}
public class PopForumsProfileGroup : ProfileGroupBase
{
public virtual bool MailingList
{
get { return ((bool)(this.GetPropertyValue("MailingList"))); }
set { this.SetPropertyValue("MailingList", value); }
}
}
Note the commented line in PopForumsProfile. That's what the generated version had, but it doesn't work, so I added the other stuff. The problem now is that while Visual Studio has no problem doing Profile.PopForums.MailingList, at runtime it says that, "The settings property for 'PopForums.MailingList' is not found." Stepping through the debugger, I can't find why this errors happens, but it's called on the set of the MailingList property. I'm not sure that the SetPropertyValue method is looking in the right place, as it doesn't appear to be calling my provider at all.
Any suggestions?
EDIT:
OK... I used Reflector to see what was going on under the hood. Here's the thing... in the implementations mentioned by Fredrik or Scott, you're calling the GetPropertyValue and SetPropertyValue methods of the ProfileBase class, which I assume in turn calls the provider and does its thing. ProfileGroupBase, on the other hand, calls the base indexer, which has no idea what to look for. Overriding the base indexer of ProfileBase to instead use the GetPropertyValue and SetPropertyValue methods actually crashes the ASP.NET worker process.
So I'm still stuck.