1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Multiple user types/db tables

Multiple user types/db tables

5 posts, 0 answered
  1. gus
    gus avatar
    4 posts
    Registered:
    10 Aug 2009
    17 Aug 2009
    Link to this post
    I need some general guidance on how to implement the following:

    users need to register under different types, I've got users that will fall into 3 categories and will require different fields to be associated with their profiles.  Basically these fields are company related and depending on the type of company they work for, they'll be required to provide different info upon registration. 

    example:  a user who works for a State will have different fields than say a self-employed person, so I'm not sure how to approach this problem using the creatuserwizard.  I suppose the proper way to set this up would be to have 3 new company-type tables and then link these table to the user profile.  My next question would be, will the sitefinity admin module accommodate multiple tables for user profiles?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    18 Aug 2009
    Link to this post
    Hello gus,

    You can modify CreateUserWizard control and create different profile data fields that will be persisted depending on a category that an user select before start filling the registration form.

    Below is a sample code:

    <sec:ManageProfile ID="ManageProfile" runat="server" Mode="Insert" GenerateButtonsInDefaultTemplate="false" > 
        <InsertTemplate> 
            <fieldset> 
             
            <asp:CheckBox ID="cb" runat="server" Text="Company" AutoPostBack="true" /> 
                <ul> 
                    <li> 
                        <asp:Label AssociatedControlID="FirstName" ID="FirstNameLabel" runat="server" Text="First name <em>(optional)</em> :" /> 
                        <asp:TextBox ID="FirstName" runat="server" /> 
                    </li> 
                    <li> 
                        <asp:Label AssociatedControlID="LastName" ID="LastNameLabel" runat="server" Text="Last name <em>(optional)</em> :" /> 
                        <asp:TextBox ID="LastName" runat="server" /> 
                    </li> 
                     <li> 
                        <asp:Label AssociatedControlID="CompanyName" ID="Label1" runat="server" Text="CompanyName <em>(optional)</em> :" /> 
                        <asp:TextBox ID="CompanyName" runat="server" /> 
                    </li> 
                     <li> 
                        <asp:Label AssociatedControlID="JobTitle" ID="Label2" runat="server" Text="JobTitle <em>(optional)</em> :" /> 
                        <asp:TextBox ID="JobTitle" runat="server" /> 
                    </li> 
                </ul> 
            </fieldset> 
        </InsertTemplate> 
    </sec:ManageProfile> 

        protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e); 
            CheckBox cb = FindControlRecursive(ManageProfile, "cb"as CheckBox; 
            TextBox cName = FindControlRecursive(ManageProfile, "CompanyName"as TextBox; 
            Label cLabel = FindControlRecursive(ManageProfile, "Label1"as Label; 
            TextBox cJobTitle = FindControlRecursive(ManageProfile, "JobTitle"as TextBox; 
     
            if (cb.Checked && Page.IsPostBack) 
            { 
                cName.Visible = true
                cLabel.Visible = true
            } 
            else 
            { 
            
                cName.Visible = false
                cLabel.Visible = false
            } 
        } 
     
        private Control FindControlRecursive(Control root, string id) 
        { 
            if (root.ID == id) 
            { 
                return root; 
            } 
     
            foreach (Control c in root.Controls) 
            { 
                Control t = FindControlRecursive(c, id); 
                if (t != null
                { 
                    return t; 
                } 
            } 
     
           

    Here we have a checkbox control. When checked we show additional TextBox controls that this user should fill. You can achieve the same functionality editing Sitefinity/Admin/CmsAdmin/Users.ascx and its code behind. It will be easy if you store different group of users in different roles because you will be able easily to determine which Priofile fileds will be visible when an user decide to edit his/her profile data.
    If you want to store Profile data in different tables you should create a custom provider, because ours looks only in telerik_Users table.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. gus
    gus avatar
    4 posts
    Registered:
    10 Aug 2009
    18 Aug 2009
    Link to this post
    acsx code works, but as soon as I modify the c# things fall apart and I'm getting the following error:

    c:\Inetpub\wwwroot\nafai.net\new\Sitefinity\UserControls\Login\CreateUserWizardControl.ascx.cs(33): error CS0161: 'CreateUserWizardControl.FindControlRecursive(System.Web.UI.Control, string)': not all code paths return a value 

    1 using System;  
    2 using System.Web;  
    3 using System.Web.UI;  
    4 using System.Web.UI.WebControls;  
    5 using System.Collections.Generic;  
    6 using System.Text;  
    7 using Telerik.Security;  
    8  
    9 public partial class CreateUserWizardControl : System.Web.UI.UserControl  
    10 {  
    11  
    12         protected override void OnLoad(EventArgs e)    
    13     {    
    14         base.OnLoad(e);    
    15         CheckBox cb = FindControlRecursive(ManageProfile, "cb"as CheckBox;    
    16         TextBox cName = FindControlRecursive(ManageProfile, "CompanyName"as TextBox;    
    17         Label cLabel = FindControlRecursive(ManageProfile, "Label1"as Label;    
    18         TextBox cJobTitle = FindControlRecursive(ManageProfile, "JobTitle"as TextBox;    
    19     
    20         if (cb.Checked && Page.IsPostBack)    
    21         {    
    22             cName.Visible = true;    
    23             cLabel.Visible = true;    
    24         }    
    25         else    
    26         {    
    27            
    28             cName.Visible = false;    
    29             cLabel.Visible = false;    
    30         }    
    31     }  
    32  
    33         private Control FindControlRecursive(Control root, string id)  
    34         {  
    35             if (root.ID == id)  
    36             {  
    37                 return root;  
    38             }  
    39  
    40             foreach (Control c in root.Controls)  
    41             {  
    42                 Control t = FindControlRecursive(c, id);  
    43                 if (t != null)  
    44                 {  
    45                     return t;  
    46                 }  
    47             }  
    48         }   
    49     
    50  
    51     protected override void OnInit(EventArgs e)  
    52     {  
    53         base.OnInit(e);  
    54  
    55         this.CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);  
    56     }  
    57  
    58     void CreateUserWizard1_CreatedUser(object sender, EventArgs e)  
    59     {  
    60               
    61     UserManager manager = new UserManager(CreateUserWizard1.MembershipProvider);  
    62         if (defaultRoles != null)  
    63         {  
    64             foreach (string role in defaultRoles)  
    65                 manager.AddUserToRole(CreateUserWizard1.UserName, role);  
    66         }  
    67         this.ManageProfile.SetProfileProperties(CreateUserWizard1.UserName);  
    68     }  
    69  
    70     public string DefaultRoles  
    71     {  
    72         get 
    73         {  
    74             StringBuilder rolesBuilder = new StringBuilder();  
    75             if (defaultRoles != null)  
    76             {  
    77                 foreach (string role in defaultRoles)  
    78                 {  
    79                     rolesBuilder.Append(role);  
    80                     rolesBuilder.Append(", ");  
    81                 }  
    82             }  
    83             if (rolesBuilder.Length >= 2)  
    84             {  
    85                 rolesBuilder.Remove(rolesBuilder.Length - 2, 2);  
    86             }  
    87             return rolesBuilder.ToString();  
    88         }  
    89         set 
    90         {  
    91             string[] roles = value.Split(',');  
    92             defaultRoles = new List<string>();  
    93             foreach (string r in roles)  
    94             {  
    95                 if (!String.IsNullOrEmpty(r.Trim()))  
    96                     defaultRoles.Add(r.Trim());  
    97             }  
    98         }  
    99     }  
    100  
    101     public CreateUserWizard CreateUserWizard  
    102     {  
    103         get 
    104         {  
    105             return this.CreateUserWizard1;  
    106         }  
    107         set 
    108         {  
    109             this.CreateUserWizard1 = value;  
    110         }  
    111     }  
    112  
    113     private List<string> defaultRoles;  
    114     
    115 }  
    116  
    117  
    118  
    119     
    120    
    121  

     

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    18 Aug 2009
    Link to this post
    Hello gus,

    As you can see form the error: " not all code paths return a value " you need to close the curly bracket and add  return null; before that which was missed because of copy-paste.

        private Control FindControlRecursive(Control root, string id) 
        { 
            if (root.ID == id) 
            { 
                return root; 
            } 
     
            foreach (Control c in root.Controls) 
            { 
                Control t = FindControlRecursive(c, id); 
                if (t != null
                { 
                    return t; 
                } 
            } 
     
            return null
        }  


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. gus
    gus avatar
    4 posts
    Registered:
    10 Aug 2009
    18 Aug 2009
    Link to this post
    thank you for your help, this is working.
Register for webinar
5 posts, 0 answered