• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

CreateUser Control

Hello,

I would like to use the CreateUser Control to add users into the asp_Membership table throught the form.

However, i need to also collect the user's First and Last Name, can this be done? If so, how?
0
asp_net2
Asked:
asp_net2
  • 10
  • 8
1 Solution
 
aibusinesssolutionsCommented:
It's easier to just make your own form with all of the information that you want to collect, and then use Membership.CreateUser() in the code behind.

You'll need to store the first and last names either in your own database table, or in a Profile table.
0
 
asp_net2Author Commented:
I just wanted to add two more fields in the asp_membership table (firstname and lastname). Can you get me started with the code using this method Membership.CreateUser().
0
 
aibusinesssolutionsCommented:
You can't add additional fields into the asp_membership table, you won't have access to them through the membership provider unless you write your own custom membership provider.  If you want to look in to doing that, here is a discussion on it. http://forums.asp.net/t/1131790.aspx

Your best bet is to store the firstname and lastname in the profile table, or you'll have to create a custom membership provider to use your own table.

Here is an example of setting up a new user with CreateUser

Imports System.Web.Security

Protected Sub CreateButton_Click()
   Dim newUser As MembershipUser
   newUser  = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text)
   Dim p As ProfileCommon = ProfileCommon.Create(UsernameTextbox.Text, True)
   p.Firstname = Firstname.Text
   p.Lastname = Lastname.Text
   p.Save
   Response.Redirect("somewhere.aspx", False)
End Sub


You'll also want to add this into your web.config file:

<profile enabled="true">
    <properties>
        <add name="Firstname" type="string"/>
        <add name="Lastname" type="string"/>
    </properties>
</profile>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
asp_net2Author Commented:
I'm having trouble with the profile enabled in my web.confi file, I'm not sure where to put it at and is this all i need to add for the profile property?

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="index.aspx.vb" Inherits="index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <label>Username:</label>
    <br />
        <asp:TextBox ID="UsernameTextbox" runat="server"></asp:TextBox>
        <br />
        <label>Password:</label>
        <br />
        <asp:TextBox ID="PasswordTextbox" runat="server"></asp:TextBox>
        <br />
        <label>First Name:</label>
        <br />
        <asp:TextBox ID="Firstname" runat="server"></asp:TextBox>
        <br />
        <label>Last Name:</label>
        <asp:TextBox ID="Lastname" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>



Imports System.Web.Profile
Imports System.Web.Security


Partial Class index
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newUser As MembershipUser
        newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text)
        Dim p As ProfileCommon = ProfileCommon.Create(UsernameTextbox.Text, True)
        p.Firstname = Firstname.Text
        p.Lastname = Lastname.Text
        p.Save()
        Response.Redirect("somewhere.aspx", False)

    End Sub
End Class
0
 
aibusinesssolutionsCommented:
It would go right after the </membership> tag

And yes, that's all you need.
0
 
asp_net2Author Commented:
Ok, having a few small problems, the data does go into the aspnet_Membership table, However i'm getting erros when trying to submit the values.

Unable to connect to SQL Server database.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3082
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.Profile;



public class ProfileCommon : System.Web.Profile.ProfileBase {
   
    public virtual string Lastname {
        get {
            return ((string)(this.GetPropertyValue("Lastname")));
        }
        set {
            this.SetPropertyValue("Lastname", value);
        }
    }
   
    public virtual string Firstname {
        get {
            return ((string)(this.GetPropertyValue("Firstname")));
        }
        set {
            this.SetPropertyValue("Firstname", value);   <------------- This line has the error
        }
    }
   
    public virtual ProfileCommon GetProfile(string username) {
        return ((ProfileCommon)(ProfileBase.Create(username)));
    }
}
0
 
aibusinesssolutionsCommented:
How is your membership provider defined in the web.config?  If you have a connection string set in it, you'll need to use the same connection string for your profile provider.
0
 
aibusinesssolutionsCommented:
Like this, make sure you change the connection string and application name to match whatever you have in the membership provider.
<profile defaultProvider="SqlProvider">
  <providers>
    <clear />
    <add name="SqlProvider"
         type="System.Web.Profile.SqlProfileProvider"
         connectionStringName="yourconnectionstring"
         applicationName="yourappname" />
  </providers>
 
  <properties>
    <add name="Firstname" />
    <add name="Lastname" />
  </properties>
</profile>

Open in new window

0
 
asp_net2Author Commented:
Okay, i'm new to the Profile provider, i have the Membership and Role Provider both working as of now but not sure what else i need to add to the Profile provider. This is what i have now.

            <membership defaultProvider="MyFirstAspNetSqlMembershipProvider">
                  <providers>
                        <add name="MyFirstAspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="MemRoles" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
                  </providers>
            </membership>
    <profile enabled="true">
      <properties>
        <add name="Firstname" type="string"/>
        <add name="Lastname" type="string"/>
      </properties>
    </profile>
            <roleManager enabled="true" cacheRolesInCookie="false" cookieName=".ASPXROLES" cookieTimeout="30" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All" defaultProvider="MyFirstAspNetSqlRoleProvider" createPersistentCookie="false" maxCachedResults="25">
                  <providers>
                        <add name="MyFirstAspNetSqlRoleProvider" connectionStringName="MemRoles" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                  </providers>
            </roleManager>
0
 
aibusinesssolutionsCommented:
Ok, so your connectionStringName is "MemRoles", and your applicationName is "/"
0
 
aibusinesssolutionsCommented:

<profile defaultProvider="SqlProvider">
  <providers>
    <clear />
    <add name="SqlProvider"
         type="System.Web.Profile.SqlProfileProvider"
         connectionStringName="MemRoles"
         applicationName="/" />
  </providers>
 
  <properties>
    <add name="Firstname" />
    <add name="Lastname" />
  </properties>
</profile>

Open in new window

0
 
asp_net2Author Commented:
Ok, that worked out great. I have a few more questions in regards to this, should be simple for you to answer :)

1.) Is there a way to retreive information in the aspnet_Membership and the aspnet_profile table after a user logins in? Such as display your Name that we just added along with there email in the aspnet_membership table?

2.) Should i create two fields in the aspnet_Profile table (firstname and lastname) or just let the go in the fields they are going in now.

3.) if i would want to add more information for the user such as there DOB, Hire Date, would i just do the same thing by add name="DOB"?
0
 
aibusinesssolutionsCommented:
1. You can easily display profile information by using the Profile class.  for example:
Label1.Text = Profile.Fullname
Label2.Text = MembershipUser.GetUser(User.Identity.Name).Email

2. Just let the profile field store the data as it does.

3. To add more profile fields, just add them to your web.config file, that will automatically add the additional fields to the profile class.
0
 
asp_net2Author Commented:
In regards to adding more profile fields. Does the web.config just send the fields to the DB as the middle man?
0
 
aibusinesssolutionsCommented:
No, the profile info is stored in fields dependent on the data type.  If they are all strings, they are stored in one field, something like Firstname:Joe|Lastname:Swanson.  asp.net does all the dirty work for you, all you have to do is add the lines to the web.config.
0
 
asp_net2Author Commented:
Okay, i added a field for Age and Date and everyting went in to the same field with my First and Last name. Is this the correct way to add to the Profile table?

1. ) Also when i want to just retrieve the AGE is it only going to pull the Age and nothing else since all other data is stored in the field.

2.) Also, how do i go about updating users data using this system?
0
 
aibusinesssolutionsCommented:
1. Yes, everything will be stored there, it will only pull the Age and nothing else.

2.  Updating profile info is as easy as doing this for the current user:
Profile.Age = 35
Profile.Save()

If you want to update Profiles for other users:
Dim p as ProfileCommon = ProfileCommon.GetUser(someuser)
p.Age = 35
p.Save()

For membership info, you'll need to do this.
Dim u as MembershipUser = Membership.GetUser(someuser)
u.Email = "me@there.com"
Membership.UpdateUser(u)
0
 
asp_net2Author Commented:
Thank you very much for all your continued help with this post
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now