Click here to Skip to main content
Click here to Skip to main content

Register user page in C# using register template

, 21 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Use the CreateUser template to create a registration page and save a new user to the database.

Introduction

This article shows how to create a user register page for your website and bind it to your database. We use the usercreate template provided by C#.

Background

There were times when people used to hand-code everything required to do a full user registration page. This involves creating an HTML interface, adding validation (password and confirm password should match, email should be of format xxx@xx.xx etc.). The newer version of .NET provides a template to quickly create one with less coding.

Using the code

Drop the 'CreateUserWizard' control from the tool box. It drops the following code on your ASPX page:

<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server" 
        ContinueDestinationPageUrl="~/Default.aspx"
        DisableCreatedUser="True"
        EmailRegularExpression="\S+@\S+\.\S+"
        EmailRegularExpressionErrorMessage="The email format is invalid." 
    FinishDestinationPageUrl="~/Default.aspx" UserNameLabelText="Login Email:" 
    UserNameRequiredErrorMessage="Login Email required." 
    oncreateduser="CreateUserWizard1_CreatedUser">
  <WizardSteps>
    <asp:CreateUserWizardStep ID="MainStep" runat="server">
      </asp:CreateUserWizardStep>
    <asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>
  </WizardSteps>
</asp:CreateUserWizard>

Go to Design view and double click the Create User button to open a code-behind event place holder, and drop the following code which saves the new user content entered in the template to the database.

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
  int nMaxId = 0; //get the latest userid and increment by 1 for the new record

  //get maxuserid from db
  System.Data.SqlClient.SqlDataReader rdr = null;
  System.Data.SqlClient.SqlConnection conn = 
      new System.Data.SqlClient.SqlConnection(
      ConfigurationManager.ConnectionStrings[
      "namodbConnectionString"].ConnectionString);
  System.Data.SqlClient.SqlCommand selcmd = 
      new System.Data.SqlClient.SqlCommand(
      "select max(usrid) maxi from usr", conn);
  try
  {
    conn.Open();
    rdr = selcmd.ExecuteReader();
    while (rdr.Read())
    { 
      nMaxId = (int)rdr["maxi"];
    }
    ++nMaxId;
  }
  finally
  {
    if (rdr != null)
    {
      rdr.Close();
    }
  }

  if (this.IsValid)
  {
    try
    {
      System.Data.SqlClient.SqlCommand insertCommand = 
          new System.Data.SqlClient.SqlCommand("Insert into [usr] (usrid," + 
          "usremail, roleid, activestat) Values ("+nMaxId+", 
          @UsrEmail, 2, 0)", conn);
      TextBox UserNameTextBox = 
         (TextBox)MainStep.ContentTemplateContainer.FindControl(
          "UserName");
      insertCommand.Parameters.Add("UsrEmail",
          SqlDbType.VarChar, 255).Value = 
          UserNameTextBox.Text;//UserGUID.ToString();
      int queryResult = insertCommand.ExecuteNonQuery();
      if (conn != null)
      {
        conn.Close();
      }
    }
    catch (Exception ex)
    {
      lblResult.Text = "Error: " + ex.Message;
    }
  }
}

Points of Interest

This tutorial guides you to quickly setup a user registration page in minutes. It also shows an easy database connection for reading data and writing data. Just to show the read data, the tutorial uses the nMaxId variable that gets its value from the max value of user ID in the table. This is an easy way to implement an auto-increment field.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

senthilvasan
Software Developer (Senior) www.namoona.com
United States United States
Founder of www.namoona.com, a multilingual classifieds portal for indian cities. Working in open systems development since 1997 in Delphi, Java, C++ and C#.

Comments and Discussions

 
Questionerror in connection Pinmemberalisedighian8-Feb-13 2:04 
QuestionAre you sure, you shoul use CreateUserWizard here? Pinmembermihasic22-Oct-08 5:08 
AnswerRe: Are you sure, you shoul use CreateUserWizard here? Pinmembersenthilvasan22-Oct-08 7:48 
GeneralClose connection PinmemberAlexey Prosyankin22-Oct-08 5:02 
GeneralRe: Close connection Pinmembersenthilvasan22-Oct-08 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 21 Oct 2008
Article Copyright 2008 by senthilvasan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid