aspnet
  1. aspnet-wf-user-registration

WF User Registration - ASP.NET Web Forms

User registration is a common requirement for websites. In this page, we will discuss how to create a user registration form in ASP.NET Web Forms using Visual Studio and SQL Server.

Prerequisites

Before we begin, you should have the following:

  • Visual Studio
  • SQL Server installed and running
  • ASP.NET Web Forms project created in Visual Studio

Steps

  1. Create a table in SQL Server to store user information, including the username, email, password, and any other information you need to collect. For example:
CREATE TABLE Users (
    UserId INT IDENTITY(1,1) PRIMARY KEY,
    Username VARCHAR(50) NOT NULL UNIQUE,
    Email VARCHAR(255) NOT NULL UNIQUE,
    Password VARCHAR(255) NOT NULL,
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50) NOT NULL
);
  1. In Visual Studio, create a new empty webpage (ASPX) file called Register.aspx.

  2. Add an HTML <form> element to the page, including textboxes for the user to enter their username, email, password, firstname and lastname, a submit button, and a link to a login page. For example:

<form id="form1" runat="server">
    <div>
        <label for="txtUsername">Username:</label>
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
        <br />

        <label for="txtEmail">Email:</label>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <br />

        <label for="txtPassword">Password:</label>
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <label for="txtFirstName">First Name:</label>
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <br />

        <label for="txtLastName">Last Name:</label>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <br />

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

        <p>
            Already have an account? <a href="Login.aspx">Login here</a>
        </p>
    </div>
</form>
  1. In the code-behind file (Register.aspx.cs), create a method to handle the submit button click event. In this method, you should validate the user's input, hash the password, and insert the user's information into the database.
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string username = txtUsername.Text.Trim();
    string email = txtEmail.Text.Trim();
    string password = txtPassword.Text.Trim();
    string firstName = txtFirstName.Text.Trim();
    string lastName = txtLastName.Text.Trim();

    // Validate input
    if (string.IsNullOrEmpty(username))
    {
        lblMessage.Text = "Please enter a username";
        return;
    }
    if (string.IsNullOrEmpty(email))
    {
        lblMessage.Text = "Please enter an email";
        return;
    }
    if (string.IsNullOrEmpty(password))
    {
        lblMessage.Text = "Please enter a password";
        return;
    }
    if (string.IsNullOrEmpty(firstName))
    {
        lblMessage.Text = "Please enter your first name";
        return;
    }
    if (string.IsNullOrEmpty(lastName))
    {
        lblMessage.Text = "Please enter your last name";
        return;
    }

    // Hash password
    string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);

    // Insert user into database
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "INSERT INTO Users (Username, Email, Password, FirstName, LastName) VALUES (@Username, @Email, @Password, @FirstName, @LastName)";
        cmd.Parameters.AddWithValue("@Username", username);
        cmd.Parameters.AddWithValue("@Email", email);
        cmd.Parameters.AddWithValue("@Password", hashedPassword);
        cmd.Parameters.AddWithValue("@FirstName", firstName);
        cmd.Parameters.AddWithValue("@LastName", lastName);

        conn.Open();
        int rowsAffected = cmd.ExecuteNonQuery();
        conn.Close();

        if (rowsAffected == 1)
        {
            lblMessage.Text = "User created successfully";
        }
        else
        {
            lblMessage.Text = "Failed to create user";
        }
    }
}
  1. Run your project and navigate to Register.aspx. You should see the registration form. Fill in the fields with valid information and click the submit button. You should see a success message if the user is created successfully.

Explanation

In this example, we created a simple user registration form in ASP.NET Web Forms and used SQL Server to store the user information. We added basic validation to ensure that the user entered all required information.

We used BCrypt.Net to hash the user's password before storing it in the database, which is important for securing user data.

Use

User registration is a common requirement for web applications. By providing a user registration form, you can allow users to create accounts and access specific features or content on your website.

Published on: