Registration form in C# with validation using SQL Server

In this tutorial, I am going to explain about " registration form" using the SQL Server database. Actually, if you are a beginner in Technology might be you are thinking that registration form is not easy to create. But don't worry if you follow the given step, it will be very easy to create form.

Here you will see 2 Phase during the web form creation. First, Database where your query logic gets placed. Second(Web Form), In this phase you will see two parts of a web form (.cs and aspx). Cs page is a server-side page where our C# code and logic is written and .aspx is a client-side page, on the client-side page our validation and design are implemented.

In this tutorial, we will also implement client-side validation with the registration form.

Step to implement a registration form

Back-end process
1. Create one table (SQL Server) in which our record will we insert.
2. Create a store-procedure to insert data.

Front-end process (WebForm Process)
1. Add connection-string in the web config file.
2. Add an webform with C# language
3. Add some textbox (for Name, Phone No, EmailID, Password)according to your requirement and add a Registration button.
4. Now, come on the cs page (code-behind page) and write the logic and code to insert the record in a database.

Now, I am going to show you step by step image format tutorial to develop registration form. [Note: I am using Visual Studio 2015]

Step 1: Open Visual Studio and click on File--->New--> Website follow the below images. registration form with sql server

Step 2: Now, Click on Template under it select C# and select Empty Web Site then give website name and location and click on OK. registration form with validation

Step 3: Find the Solution Explorer and Right-click on Website and add webForm with a name Registration. See in below image. registration form with procedure

Step 4: Add some TextBoxes and One Button to insert the record in the DataBase. And Design the webForm.

Registration form in  with sql procedure

Registration.aspx code (Copy and paste it in your web form under <div> </div> section)

                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                    <asp:RequiredFieldValidator ControlToValidate="txtname" Display="Dynamic" ErrorMessage="Enter Name" ForeColor="Red" ID="req1" runat="server" ValidationGroup="Save"></asp:RequiredFieldValidator>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
                    <asp:RequiredFieldValidator ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="Enter Email" ForeColor="Red" ID="RequiredFieldValidator1" runat="server" ValidationGroup="Save"></asp:RequiredFieldValidator>

                        <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
                    <asp:RequiredFieldValidator ControlToValidate="txtAddress" Display="Dynamic" ErrorMessage="Enter Address" ForeColor="Red" ID="RequiredFieldValidator2" runat="server" ValidationGroup="Save"></asp:RequiredFieldValidator>

                        <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
                    <asp:RequiredFieldValidator ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="Password" ForeColor="Red" ID="RequiredFieldValidator3" runat="server" ValidationGroup="Save"></asp:RequiredFieldValidator>

                        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Register" ValidationGroup="Save" />


Step 5: Add connection in WebConfig under <configuration> tag

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
      <add name="MyCon" connectionString="initial catalog=Test; integrated security=true; data source=." />

Step 6: Create Table and procedure in MS SQL server

crate database Test
use Test
create table user_Registration
UID int identity(1,1),
UName varchar(50),
UEmail varchar(50),
UAddress varchar(50),
UPassword varchar(50)

create proc sp_Insert
@Name varchar(50),
@Email varchar(50),
@Address varchar(50),
@Password varchar(50)
  insert into user_Registration (UName,UEmail,UAddress,UPassword) values(@Name,@Email,@Address,@Password)

 Step: 7

.Cs page 

Write code on Button button_Onclick event

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class Registration : System.Web.UI.Page
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)

    protected void btnSave_Click(object sender, EventArgs e)
        SqlCommand cmd = new SqlCommand("sp_Insert", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", txtname.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
            Response.Write("<script> alert('Registered Sucessfully'); </script>");

Finally, Record inserted by the registration. If you get any problem Please feel free to comment below.