Click here to Skip to main content
15,894,907 members
Articles / Web Development / ASP.NET

Register user page in C# using register template

Rate me:
Please Sign up or sign in to vote.
2.33/5 (5 votes)
21 Oct 2008CPOL1 min read 58.8K   10   5
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.NET
<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.

C#
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)


Written By
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 Pin
ali sedighian8-Feb-13 2:04
ali sedighian8-Feb-13 2:04 
QuestionAre you sure, you shoul use CreateUserWizard here? Pin
mihasic22-Oct-08 5:08
mihasic22-Oct-08 5:08 
AnswerRe: Are you sure, you shoul use CreateUserWizard here? Pin
senthilvasan22-Oct-08 7:48
senthilvasan22-Oct-08 7:48 
GeneralClose connection Pin
Oleksii Prosiankin22-Oct-08 5:02
Oleksii Prosiankin22-Oct-08 5:02 
In the second try-catch block if you have got any exception you have left open connection. Close connection in finally block!
GeneralRe: Close connection Pin
senthilvasan22-Oct-08 7:42
senthilvasan22-Oct-08 7:42 

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

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