lazell
lazell

Reputation: 53

ASP.Net Using the Membership.CreateUser Method

I'm having issues using the membership.createuser method. My code is below, I get a null exception as runtime. Any help is greatly appreciated.

    Dim username As String = userNameTxt.Text
    Dim password As String = passwordTxt.Text
    Dim email As String = emailTxt.Text
    Dim status As New MembershipCreateStatus

    Dim blank As String = ""
    Dim provider As MembershipProvider
    Dim providerUserKey As New System.Object

    Dim user As MembershipUser
    user = provider.CreateUser(username, password, email, blank, blank, True, providerUserKey, status)

Upvotes: 0

Views: 5279

Answers (3)

Richard Friend
Richard Friend

Reputation: 16018

 Dim provider As MembershipProvider 

then

provider.CreateUser(..

provider is null as you haven't actually created a new instance

you should be using System.Web.Security.Membership.CreateUser

Upvotes: 0

Kai G
Kai G

Reputation: 3512

Use the shared methods on the Membership class. You will have to configure the membership provider to use in your web.config file.

Then it's simply:

Membership.CreateUser(username, password, email, blank, blank, True, providerUserKey, status)

For details on how to configure your membership provider, refer to msdn Memberhip doc

Upvotes: 0

Robert
Robert

Reputation: 3302

you need concrete implementation of MembershipProvider abstract class you can either create your own or use existing one. You also need to set it in web.config:

<connectionStrings>
  <add name="MySqlConnection" connectionString="Data Source=MySqlServer;Initial Catalog=aspnetdb;Integrated Security=SSPI;" />
</connectionStrings>
<system.web>
...
  <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
    <providers>
      <clear />
      <add 
        name="SqlProvider" 
        type="System.Web.Security.SqlMembershipProvider" 
        connectionStringName="MySqlConnection"
        applicationName="MyApplication"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="true"
        requiresUniqueEmail="true"
        passwordFormat="Hashed" />
    </providers>
  </membership>

http://msdn.microsoft.com/en-us/library/ff648345.aspx

then you can use Membership.CreateUser, no need to create instance, it's static it has also a property for default membership provider set in web.config: Membership.Provider

Upvotes: 2

Related Questions