Custom Member Properties: datepicker from codebehind
I have followed Mike Taylor's excellent blog post regarding creating/authenticating members and accessing custom properties and everything works great (Thanks Mike!). I have tried to extend it a little bit and access a datepicker custom member property (dateofbirth) and set the value when creating members. The issue is with object types, but I am still learning .net, so I am sure it is something simple I have overlooked and a fresh set of eyes would be welcome! BTW, if I set the property to 'textstring' in the backend, it works fine, but when it is set to 'datepicker' it explodes. Here is the code:
Usercontrol (excerpt):
<input runat="server" type="text" class="datepicker form-control" id="DateOfBirth" placeholder="Date Of Birth">
Codebehind (excerpt):
protected void cwMember_CreatedUser(object sender, EventArgs e)
{
CreateUserWizard cuw = (CreateUserWizard)sender;
MembershipUser user = Membership.GetUser(cuw.UserName);
if (user != null)
{
// create a new Guid
string newUserGuid = System.Guid.NewGuid().ToString("N");
// get the profile for this user
MemberProfile mp = MemberProfile.GetUserProfile(cuw.UserName);
mp.AuthGuid = newUserGuid;
mp.FirstName = ((TextBox)cuw.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
mp.MiddleName = ((TextBox)cuw.CreateUserStep.ContentTemplateContainer.FindControl("MiddleName")).Text;
mp.LastName = ((TextBox)cuw.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
mp.DateOfBirth = ((HtmlInputText)cuw.CreateUserStep.ContentTemplateContainer.FindControl("DateOfBirth")).Value;
mp.Save();
And code to expose the properties (excerpt):
[SettingsAllowAnonymous(false)]
public string DateOfBirth
{
get
{
var o = base.GetPropertyValue("member_dateofbirth");
if (o == DBNull.Value)
{
return string.Empty;
}
return (string)o;
}
set
{
base.SetPropertyValue("member_dateofbirth", value);
}
}
And finally, properties in the web.config (excerpt)
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not
valid.
Source Error:
Line 33: mp.LastName =
((TextBox)cuw.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
Line 34: mp.DateOfBirth =
((HtmlInputText)cuw.CreateUserStep.ContentTemplateContainer.FindControl("DateOfBirth")).Value;
Line 35: mp.Save(); Line 36: Line 37:
// add the user to the "Public" group
If I change the type in the web.config from System.String to System.DateTime for the member_dateofbirth property it then dies as such:
The settings property 'member_dateofbirth' is of a non-compatible type.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details:
System.Configuration.SettingsPropertyWrongTypeException: The settings
property 'member_dateofbirth' is of a non-compatible type.
Source Error:
Line 104: set Line 105: { Line 106:
base.SetPropertyValue("member_dateofbirth", value); Line 107:
} Line 108: }
It is obvious that the error is with types, I just can't figure out the correct way to set things to make it work! If all else fails, I can just set the property to be a textsring in the backend, but I would much prefer to have it stored as a date.
and I also needed to correctly type the property being exposed:
[SettingsAllowAnonymous(false)]
public DateTime DateOfBirth
{
get
{
var o = base.GetPropertyValue("member_dateofbirth");
if (o == DBNull.Value)
{
return new DateTime();
}
return (DateTime)o;
}
set
{
base.SetPropertyValue("member_dateofbirth", value);
}
}
}
Now all is good and working properly! Boy it sure helps to read the documentation!
I am on v6.1.3 and that is what I though, but if I comment out the <properties>...</properties> section (or just the offending lines) in the web.config I end up with errors like this
The settings property 'auth_guid' was not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Configuration.SettingsPropertyNotFoundException: The settings property 'auth_guid' was not found.
Source Error:
Line 32: set
Line 33: {
Line 34: base.SetPropertyValue("auth_guid", value);
Line 35: }
Line 36: }
Also in the class that has the properties in should this not be inherting from ProfileBase?. You should create a new ProfileBase for the user and set the properties agiasnt that users profile? If that makes sense. Charlie.
The class is pretty much word or word from Mike's blog, all I added was the bit for also storing a DateTime value for the 'Date of Birth' field.
using System;
using System.Web.Profile;
using System.Web.Security;
namespace MT.Flag.UmbracoTests
{
public class MemberProfile : ProfileBase
{
public static MemberProfile GetUserProfile(string username)
{
return Create(username) as MemberProfile;
}
public static MemberProfile GetUserProfile()
{
return Create(Membership.GetUser().UserName) as MemberProfile;
}
[SettingsAllowAnonymous(false)]
public string AuthGuid
{
get
{
var o = base.GetPropertyValue("auth_guid");
if (o == DBNull.Value)
{
return string.Empty;
}
return (string)o;
}
set
{
base.SetPropertyValue("auth_guid", value);
}
}
[SettingsAllowAnonymous(false)]
public string FirstName
{
get
{
var o = base.GetPropertyValue("member_firstname");
if (o == DBNull.Value)
{
return string.Empty;
}
return (string)o;
}
set
{
base.SetPropertyValue("member_firstname", value);
}
}
[SettingsAllowAnonymous(false)]
public string LastName
{
get
{
var o = base.GetPropertyValue("member_lastname");
if (o == DBNull.Value)
{
return string.Empty;
}
return (string)o;
}
set
{
base.SetPropertyValue("member_lastname", value);
}
}
[SettingsAllowAnonymous(false)]
public string MiddleName
{
get
{
var o = base.GetPropertyValue("member_middlename");
if (o == DBNull.Value)
{
return string.Empty;
}
return (string)o;
}
set
{
base.SetPropertyValue("member_middlename", value);
}
}
[SettingsAllowAnonymous(false)]
public DateTime DateOfBirth
{
get
{
var o = base.GetPropertyValue("member_dateofbirth");
if (o == DBNull.Value)
{
return new DateTime();
}
return (DateTime)o;
}
set
{
base.SetPropertyValue("member_dateofbirth", value);
}
}
}
}
The 'date of birth' property is defined as a datepicker in the backend.
I am still staring at your blog trying to figure out if I missed something because if I remove the properties section in the web config it still blows up. Here is the error and stack trace with the properties section removed:
Server Error in '/' Application.
The settings property 'auth_guid' was not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Configuration.SettingsPropertyNotFoundException: The settings property 'auth_guid' was not found.
Source Error:
Line 32: set
Line 33: {
Line 34: base.SetPropertyValue("auth_guid", value);
Line 35: }
Line 36: }
Source File: d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\MemberProfile.cs Line: 34
Stack Trace:
[SettingsPropertyNotFoundException: The settings property 'auth_guid' was not found.]
System.Configuration.SettingsBase.SetPropertyValueByName(String propertyName, Object propertyValue) +1388883
System.Configuration.SettingsBase.set_Item(String propertyName, Object value) +126
System.Web.Profile.ProfileBase.SetInternal(String propertyName, Object value) +141
System.Web.Profile.ProfileBase.set_Item(String propertyName, Object value) +80
System.Web.Profile.ProfileBase.SetPropertyValue(String propertyName, Object propertyValue) +13
MT.Flag.UmbracoTests.MemberProfile.set_AuthGuid(String value) in d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\MemberProfile.cs:34
MT.Flag.UmbracoTests.usercontrols.memberRegister.cwMember_CreatedUser(Object sender, EventArgs e) in d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\memberRegister.ascx.cs:30
System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs e) +116
System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +342
System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +110
System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +401
System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.WebControls.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) +16
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +159
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044
The name and alias for auth_guid is Auth_GUID and auth_guid in the backend. I just realized that that could have the same naming issue as the other properties, so I changed the alias to member__auth_guid and changed the references in the class to match. Still gettting the same errors when I remove the properties section from the web.config
I also took a look at the return(DateTime)o; line and it seems to be OK. The form is using a <input type="text"> to store the date (using bootstrap-datepicker.js to make the text input act like a nice datepicker) as a string. When I look at the line var o = base.GetPropertyValue("member_dateofbirth"); in VS Express it says it is a string and there are no compile errors. Also, it works! (I know that doesn't mean it is right, but it does provide some weight to it being correct).
Hi Charles, I have changed the name and alias for member_auth_guid so that they are now different. I had missed that before. I still can't remove the properties from the web.config, but I am not sure what the advantage of doing so would be? I am sure there is one, but is it so terribly to have the properties in the web.config?
At the moment, it is working as expected. Visual Studio is compiling everything happily and not throwing any errors, or complaining in any way. Members are being created, properties are being applied, and life is good. Thanks for all the help, and if you can tell me a reason to keep arguing with the web.config file, I will be happy to, otherwise I am OK with leaving it as it is.
Custom Member Properties: datepicker from codebehind
I have followed Mike Taylor's excellent blog post regarding creating/authenticating members and accessing custom properties and everything works great (Thanks Mike!). I have tried to extend it a little bit and access a datepicker custom member property (dateofbirth) and set the value when creating members. The issue is with object types, but I am still learning .net, so I am sure it is something simple I have overlooked and a fresh set of eyes would be welcome! BTW, if I set the property to 'textstring' in the backend, it works fine, but when it is set to 'datepicker' it explodes. Here is the code:
Usercontrol (excerpt):
Codebehind (excerpt):
And code to expose the properties (excerpt):
And finally, properties in the web.config (excerpt)
The error and Stack Trace:
If I change the type in the web.config from System.String to System.DateTime for the member_dateofbirth property it then dies as such:
It is obvious that the error is with types, I just can't figure out the correct way to set things to make it work! If all else fails, I can just set the property to be a textsring in the backend, but I would much prefer to have it stored as a date.
Cheers!
Josh
Ok, so I am a fool. I have solved the issue by using the correct types in the correct places!
In the codebehind, I needed to parse the string to convert it to a DateTime:
and I also needed to correctly type the property being exposed:
Now all is good and working properly! Boy it sure helps to read the documentation!
What version of Umbraco are you using? You should not need all of those properties defined in webconfig. Charlie :)
I am on v6.1.3 and that is what I though, but if I comment out the
<properties>...</properties>
section (or just the offending lines) in the web.config I end up with errors like thisAny suggestions?
Ah i think i see the problem can you tlell em what the property name is and what the property alias is? Charlie.
Also in the class that has the properties in should this not be inherting from ProfileBase?. You should create a new ProfileBase for the user and set the properties agiasnt that users profile? If that makes sense. Charlie.
Hey Charlie, everything follows the same naming convention, so here is an example.
In the backend: Property Name - 'First Name' Property Alias - 'member_firstname'
I went through the pain of realizing that I needed to change the property alias as per the comments on Mike's blog post which I was following.
Brilliant, and ha yes that was a real pain to figure out! Can you remove the stuff from the web config? And what error do you get then?
Have a look http://charlesafford.com/umbraco-membership-provider.aspx
That will work without webconfig
The class is pretty much word or word from Mike's blog, all I added was the bit for also storing a DateTime value for the 'Date of Birth' field.
ah i see ok, what type is date of birth? and you dont need the properties section in web config. Should work fine with out it
The 'date of birth' property is defined as a datepicker in the backend.
I am still staring at your blog trying to figure out if I missed something because if I remove the properties section in the web config it still blows up. Here is the error and stack trace with the properties section removed:
I think at the moment this line
return(DateTime)o;
Is trying to case a string to be a DateTime
Are you using VisualStudio? if so dont use the var keyword. Use string intead.
Ok what is the alias and name for auth_guid
The name and alias for auth_guid is
Auth_GUID
andauth_guid
in the backend. I just realized that that could have the same naming issue as the other properties, so I changed the alias tomember__auth_guid
and changed the references in the class to match. Still gettting the same errors when I remove the properties section from the web.configI also took a look at the
return(DateTime)o;
line and it seems to be OK. The form is using a<input type="text">
to store the date (using bootstrap-datepicker.js to make the text input act like a nice datepicker) as a string. When I look at the linevar o = base.GetPropertyValue("member_dateofbirth");
in VS Express it says it is a string and there are no compile errors. Also, it works! (I know that doesn't mean it is right, but it does provide some weight to it being correct).Now I have confused myself. It is not a string... it is an object of DateTime.
Ok
is the name and the alias the same for member__auth_guid
These have to be diffrent.
So for the name use
member__auth_guid
for the alias use
_member__auth_guid
You should have a cast error because the GetProperty is returning a string not a DateTime object. It's safer to DateTime.TryParse.
Hi Charles, I have changed the name and alias for
member_auth_guid
so that they are now different. I had missed that before. I still can't remove the properties from the web.config, but I am not sure what the advantage of doing so would be? I am sure there is one, but is it so terribly to have the properties in the web.config?At the moment, it is working as expected. Visual Studio is compiling everything happily and not throwing any errors, or complaining in any way. Members are being created, properties are being applied, and life is good. Thanks for all the help, and if you can tell me a reason to keep arguing with the web.config file, I will be happy to, otherwise I am OK with leaving it as it is.
Cheers! Josh
is working on a reply...