Click here to Skip to main content
12,702,980 members (30,808 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Register user page in C# using register template

, 21 Oct 2008 CPOL
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.


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#.


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" 
        EmailRegularExpressionErrorMessage="The email format is invalid." 
    FinishDestinationPageUrl="~/Default.aspx" UserNameLabelText="Login Email:" 
    UserNameRequiredErrorMessage="Login Email required." 
    <asp:CreateUserWizardStep ID="MainStep" runat="server">
    <asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>

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(
  System.Data.SqlClient.SqlCommand selcmd = 
      new System.Data.SqlClient.SqlCommand(
      "select max(usrid) maxi from usr", conn);
    rdr = selcmd.ExecuteReader();
    while (rdr.Read())
      nMaxId = (int)rdr["maxi"];
    if (rdr != null)

  if (this.IsValid)
      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 = 
          SqlDbType.VarChar, 255).Value = 
      int queryResult = insertCommand.ExecuteNonQuery();
      if (conn != null)
    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.


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


About the Author

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

You may also be interested in...

Comments and Discussions

Questionerror in connection Pin
alisedighian8-Feb-13 3:04
memberalisedighian8-Feb-13 3:04 
QuestionAre you sure, you shoul use CreateUserWizard here? Pin
mihasic22-Oct-08 6:08
membermihasic22-Oct-08 6:08 
AnswerRe: Are you sure, you shoul use CreateUserWizard here? Pin
senthilvasan22-Oct-08 8:48
membersenthilvasan22-Oct-08 8:48 
GeneralClose connection Pin
Alexey Prosyankin22-Oct-08 6:02
memberAlexey Prosyankin22-Oct-08 6:02 
GeneralRe: Close connection Pin
senthilvasan22-Oct-08 8:42
membersenthilvasan22-Oct-08 8: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.

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