---
title: "User Registration generate MemberShipException in asp.net"  
description: "User Registration generate MemberShipException in asp.net"  
author: "Anonymous User"  
published: 2014-11-02  
updated: 2014-11-03  
canonical: https://www.mindstick.com/forum/2492/user-registration-generate-membershipexception-in-asp-dot-net  
category: "asp.net"  
tags: ["c#", "asp.net mvc", "exception", "asp.net membership"]  
reading_time: 3 minutes  

---

# User Registration generate MemberShipException in asp.net

I'm using the [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) 4 [template](https://www.mindstick.com/blog/282/creating-custom-template-in-joomla) to create my [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), in my method [Register](https://www.mindstick.com/blog/220/registering-client-script-in-asp-dot-net) POST, i put a block that call the UserProfile and save new information, on this case, it will save UserType.

The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is: Every time that I'll save the user, the [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) MembershipCreateUserException is called and the following [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) is displayed on the page: [User name](https://www.mindstick.com/interview/33836/how-to-check-current-user-name-of-database-in-sql-server) [already exists](https://www.mindstick.com/forum/34205/emailid-already-exists). Please enter a different user name. The User is saved on Database.

## This is my method Register:

```
public ActionResult Register(RegisterModel model)        {            if (ModelState.IsValid)            {                // Attempt to register the user                try                {                   // var user = new UserProfile() { UserName = model.UserName };                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);                    WebSecurity.Login(model.UserName, model.Password);                    return RedirectToAction("Index", "Home");                }                catch (MembershipCreateUserException e)                {                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));                }//new code                using (UsersContext db = new UsersContext())                {                    // Insert name into the profile table                    UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName, UserType = model.UserType });                    db.SaveChanges();                }            }
```

Anybody knows how can i resolve it? Every help will be valid! Thanks guys.\

## Replies

### Reply by Anonymous User

The following line adds a new user:

```
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
```

And this line does it again:

```
UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName, UserType = model.UserType });
```

Try instead the following to add the user type when creating the user, make sure UserType is part of your RegisterModel model:

```
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserType = model.UserType});
```

So the entire Register method will look like this:

```
public ActionResult Register(RegisterModel model)        {            if (ModelState.IsValid)            {                //Attempt to register the user                 try                {                  
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserType = model.UserType
});                    return RedirectToAction("Index", "Home");                 }                catch (MembershipCreateUserException e)                {                    ModelState.AddModelError("",ErrorCodeToString(e.StatusCode));                }            }
```


---

Original Source: https://www.mindstick.com/forum/2492/user-registration-generate-membershipexception-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
