Though ASP.NET controls are great, in many cases I prefer the power and flexibility of doing things in code. 

I wrote the following code this week as part of a demo I’m working on and thoght I would share it. (Why write all these case statements yourself.)

I hope someone finds it useful.

   1:  public partial class NewUser : System.Web.UI.Page
   2:  {
   3:      protected void Page_Load(object sender, EventArgs e)
   4:      {
   5:       Session["ContinueDestinationPageUrl"] = Request.QueryString["ReturnUrl"];
   6:      }
   7:   
   8:      protected void btnAddUser_Click(object sender, EventArgs e)
   9:      {
  10:          if (Page.IsValid)
  11:          {
  12:              MembershipCreateStatus createStatus;
  13:              string sUserName = tbUserName.Text;
  14:              string sPassword = tbPassword.Text;
  15:              string sEmail = tbEmailAddress.Text;
  16:              string sQuestion = tbSecurityQuestion.Text;
  17:              string sAnswer = tbSecurityAnswer.Text;
  18:   
  19:              MembershipUser newUser = Membership.CreateUser(
                                            HttpUtility.HtmlEncode(sUserName.Trim()),
  20:                                       HttpUtility.HtmlEncode(sPassword.Trim()),
  21:                                        HttpUtility.HtmlEncode(sEmail.Trim()),
  22:                                       HttpUtility.HtmlEncode(sQuestion.Trim()),
  23:                                       HttpUtility.HtmlEncode(sAnswer.Trim()),
  24:                                       true,
  25:                                        out createStatus);
  26:              string CreateResultMessage = "";
  27:              switch (createStatus)
  28:              {
  29:                  case MembershipCreateStatus.Success:
  30:                      CreateResultMessage =
                                  "» The user was successfully created. «";
  31:                      break;
  32:                  case MembershipCreateStatus.InvalidUserName:
  33:                      CreateResultMessage =
                                   "The user name was not found in the database.";
  34:                      break;
  35:                  case MembershipCreateStatus.InvalidPassword:
  36:                      CreateResultMessage =
                                   "The password is not formatted correctly.";
  37:                      break;
  38:                  case MembershipCreateStatus.InvalidQuestion:
  39:                      CreateResultMessage =
                                   "The password question is not formatted correctly.";
  40:                      break;
  41:                  case MembershipCreateStatus.InvalidAnswer:
  42:                      CreateResultMessage =
                                  "The password answer is not formatted correctly.";
  43:                      break;
  44:                  case MembershipCreateStatus.InvalidEmail:
  45:                      CreateResultMessage =
                                 "The e-mail address is not formatted correctly.";
  46:                      break;
  47:                  case MembershipCreateStatus.DuplicateUserName:
  48:                      CreateResultMessage =
                                  "The user name already exists in the database
                                                  for the application.";
  49:                      break;
  50:                  case MembershipCreateStatus.DuplicateEmail:
  51:                      CreateResultMessage =
                                  "The e-mail address already exists in the database
                                                  for the application.";
  52:                      break;
  53:                  case MembershipCreateStatus.UserRejected:
  54:                      CreateResultMessage =
                                   "The user was not created, for a reason defined
                                              by the provider.";
  55:                      break;
  56:                  case MembershipCreateStatus.InvalidProviderUserKey:
  57:                      CreateResultMessage =
                                   "The provider user key is of an invalid type or
                                              format.";
  58:                      break;
  59:                  case MembershipCreateStatus.DuplicateProviderUserKey:
  60:                      CreateResultMessage =
                                    "The ProviderUserKey already exists in the database
                                              for the application.";
  61:                      break;
  62:                  case MembershipCreateStatus.ProviderError:
  63:                      CreateResultMessage =
                                "The provider returned an error that is not described
                                                  by other ";
  64:                      CreateResultMessage +=
                                    "MembershipCreateStatus enumeration values.";
  65:                      break;
  66:              }
  67:   
  68:              if (createStatus != MembershipCreateStatus.Success)
  69:              {
  70:                  lblCreateResultMessage.CssClass = "ValidationError";
  71:                  lblCreateResultMessage.Text = CreateResultMessage;
  72:                  btnAddUser.Visible = true;
  73:                  divResultMessage.Visible = true;
  74:              }
  75:              else
  76:              {
  77:                  lblCreateResultMessage.CssClass = "bold";
  78:                  btnAddUser.Visible = false;
  79:                  FormsAuthentication.SetAuthCookie(newUser.UserName, false);
  80:   
  81:                  string continueUrl =
                              Session["ContinueDestinationPageUrl"].ToString();
  82:                  if (String.IsNullOrEmpty(continueUrl))
  83:                  {
  84:                      continueUrl = "~/";
  85:                  }
  86:                  Response.Redirect(continueUrl);
  87:              }
  88:   
  89:          }
  90:      }
  91:   
  92:  }

Technorati Tags: ,,,
Article Global Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google Yahoo Buzz StumbleUpon Eli Pets