Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 10:39
    Josh Olson
    0

    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)

     <properties>
        <clear/>
        <add name="auth_guid" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String"/>
        <add name="member_firstname" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String"/>
        <add name="member_lastname" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String"/>
        <add name="member_middlename" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String"/>
        <add name="member_dateofbirth" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String"/>
      </properties>
    

    The error and Stack Trace:

    Specified cast is not valid.

    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

    Source File: d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\memberRegister.ascx.cs Line: 35

    Stack Trace:

    [InvalidCastException: Specified cast is not valid.]
    umbraco.editorControls.datepicker.DateData.ToXMl(XmlDocument d) +90
    umbraco.cms.businesslogic.property.Property.ToXml(XmlDocument xd) +153 umbraco.cms.businesslogic.Content.XmlPopulate(XmlDocument xd, XmlNode& x, Boolean Deep) +141
    umbraco.cms.businesslogic.member.Member.generateXmlWithoutSaving(XmlDocument xd) +65 umbraco.cms.businesslogic.Content.XmlGenerate(XmlDocument xd) +15 umbraco.cms.businesslogic.member.Member.Save() +1483
    umbraco.providers.members.UmbracoProfileProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) +460
    System.Configuration.SettingsBase.SaveCore() +389
    System.Configuration.SettingsBase.Save() +114
    System.Web.Profile.ProfileBase.SaveWithAssert() +31
    System.Web.Profile.ProfileBase.Save() +72
    MT.Flag.UmbracoTests.usercontrols.memberRegister.cwMember_CreatedUser(Object sender, EventArgs e) in d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\memberRegister.ascx.cs:35 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

    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: }

    Source File: d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\MemberProfile.cs Line: 106

    Stack Trace:

    [SettingsPropertyWrongTypeException: The settings property 'memberdateofbirth' is of a non-compatible type.]
    System.Configuration.SettingsBase.SetPropertyValueByName(String propertyName, Object propertyValue) +1389043
    System.Configuration.SettingsBase.set
    Item(String propertyName, Object value) +126 System.Web.Profile.ProfileBase.SetInternal(String propertyName, Object value) +141
    System.Web.Profile.ProfileBase.setItem(String propertyName, Object value) +80 System.Web.Profile.ProfileBase.SetPropertyValue(String propertyName, Object propertyValue) +13
    MT.Flag.UmbracoTests.MemberProfile.set
    DateOfBirth(String value) in d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\MemberProfile.cs:106 MT.Flag.UmbracoTests.usercontrols.memberRegister.cwMember_CreatedUser(Object sender, EventArgs e) in d:\Web Sites\RegisterAndValidate\RegisterAndValidate\WebApplication1\memberRegister.ascx.cs:34 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

    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

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 11:18
    Josh Olson
    100

    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:

    mp.DateOfBirth = DateTime.Parse(((HtmlInputText)cuw.CreateUserStep.ContentTemplateContainer.FindControl("DateOfBirth")).Value);
    

    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!

  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:09
    Charles Afford
    0

    What version of Umbraco are you using?  You should not need all of those properties defined in webconfig.  Charlie :)

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 12:15
    Josh Olson
    0

    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:         }
    

    Any suggestions?

  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:17
    Charles Afford
    0

    Ah i think i see the problem can you tlell em what the property name is and what the property alias is?  Charlie.

  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:23
    Charles Afford
    0

    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.

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 12:24
    Josh Olson
    0

    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.

  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:26
    Charles Afford
    0

    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

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 12:27
    Josh Olson
    0

    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);
                }
            }
        }
    }
    
  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:31
    Charles Afford
    0

    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

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 12:35
    Josh Olson
    0

    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
    
  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:38
    Charles Afford
    0

    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.

     

    string o =base.GetPropertyValue("member_dateofbirth"); //this will return a string
    now when you try to case o as DateTime you should get a compile error
    return(DateTime)o;
    I think you need return DateTime.Parse(o);
    or to be safe you should use
    DateTime.TryParse(o, out somestring);
  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 12:41
    Charles Afford
    0

    Ok what is the alias and name for auth_guid

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 12:59
    Josh Olson
    0

    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).

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 13:02
    Josh Olson
    0

    Now I have confused myself. It is not a string... it is an object of DateTime.

  • Charles Afford 1163 posts 1709 karma points
    Sep 06, 2013 @ 14:00
    Charles Afford
    0

    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.

  • Josh Olson 79 posts 207 karma points
    Sep 06, 2013 @ 14:06
    Josh Olson
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft