title: How do I get User ID for new registered user in ASP.Net CreateUserWizard?
body: <p>How do I get User ID for new registered user in ASP.Net CreateUserWizard?</p>
  
  <p>I have a page that allows new users to register with a Create User Wizard. Right after a user is created I would like to insert a row in a customer details table using the new user's User ID and email. </p>
  
  <p>I tried putting some code in the CreateUserWizard1_CreatedUser to get the User ID. But, from what I learned, the user is created at this point; but, the user is not logged in at this point. I get an error that says...</p>
  
  <p>"NullReferenceException" was handled by user code.
  Object not set to an instance of an object."</p>
  
  <p>If I put a breakpoint in during debugging I can see that the value of the customerId is null.</p>
  
  <p>From what I read on the MSDN site, the CreatedUser event...
  "Occurs after the membership provider has created the new Web site user account."</p>
  
  <p>I want to add the row to the customer details table before the Create User Wizard changes to display the "Continue" button. At that point, the user can go to other pages or close the application. </p>
  
  <p>Is there some other event after the CreatedUser where I can put some code?
  I see that there is an Unload event. 
  <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard_events%28v=vs.110%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard_events%28v=vs.110%29.aspx</a></p>
  
  <p>Can I can somehow log in the user in the CreatedUser event to get the User ID?</p>
  
  <hr>
  
  <p>Here is my code on the Register page that isn't working. The error occurs on the line of code: "string customerId ="</p>
  
  <pre><code>    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
  {
      // Add the Role of Customers to the new user.
      Roles.AddUserToRole((sender as CreateUserWizard).UserName, "Customers");
  
      // Get the current user's ID and email
      string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
      string email = Membership.GetUser(HttpContext.Current.User.Identity.Name).Email.ToString();
      bool success = CustomerDetailsAccess.CreateCustomerDetails(customerId, email);
  </code></pre>
  
  <p>... </p>
  
  <pre><code>}
  </code></pre>