How to Make a Registration Form Having no Duplicate Username

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide it a suitable name (RegForm_demo).

Step 2

In the Solution Explorer you get your empty website, then add two web forms and a SQL Server database as in the following.

For Web Form

RegForm_demo (your empty website), right-click and Add New Item, then select Web Form and name it Reg_demo.aspx. Repeat the same process and add another web form and name it (Redirectpage.aspx)

For SQL Server Database

RegForm_demo (your empty website), right-click and Add New Item, then select SQL Server Database. (Add a database inside the App_Data_folder.)

Database Chamber

Step 3

In Server Explorer, click on your database (Database.mdf), go to Tables and Add New Table, create a table like the following:

Make table

Make a Stored Procedure for inserting data into the database and for checking the database for the same username or not.

  1. Sp_check:

    check

  2. Sp_insert:

    insert

Design Code

Step 4

Now make some design for your application by going to Reg_demo.aspx and try the code like the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2.   
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10.     <style type="text/css">  
  11.         .style1  
  12.         {  
  13.             width: 199px;  
  14.         }  
  15.         .style2  
  16.         {  
  17.             width: 183px;  
  18.         }  
  19.          div.backgroud  
  20.   {  
  21.       width:500px;  
  22.       height:250px;  
  23.       background-color: #A9A9A9;  
  24.       
  25.   }  
  26.   div.box  
  27.   {  
  28.       width:500px;  
  29.       height:380px;  
  30.       margin:100px 400px 100px 300px;  
  31.       background-color: #FAFAD2;  
  32.       padding:20px 150px 50px 30px;  
  33.       opacity:0.8;  
  34.        -moz-box-shadow: 10px 10px 5px #888888;  
  35.        box-shadow: 10px 10px 5px #888888;  
  36.        border:1px solid black;  
  37.   }  
  38.   .tbl  
  39.   {  
  40.       margin: 50px 50px 50px 50px;      
  41.     }        
  42.         .style4  
  43.         {  
  44.             text-decoration: underline;  
  45.             font-size: x-large;  
  46.             margin:0px 100px 0px 0px;  
  47.         }        
  48.         .style6  
  49.         {  
  50.             width: 268px;  
  51.         }  
  52.         .style7  
  53.         {  
  54.             width: 338px;  
  55.         }  
  56.           
  57.    </style>  
  58. </head>  
  59. <body>  
  60.     <form id="form1" runat="server">  
  61.       
  62.     <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  63.     </asp:ToolkitScriptManager>  
  64.     <div class="background">  
  65.     <div class="box">      
  66.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  67.         <ContentTemplate>  
  68.           
  69.             <table class="tbl" style="width:100%;">  
  70.                 <caption class="style4" align="top">  
  71.                     <strong>Registration Form</strong></caption>  
  72.                 <tr>  
  73.                     <td class="style1">  
  74.                          </td>  
  75.                     <td class="style2">  
  76.                          </td>  
  77.                     <td class="style7">  
  78.                          </td>  
  79.                 </tr>  
  80.                 <tr>  
  81.                     <td class="style1">  
  82.                          </td>  
  83.                     <td class="style2">  
  84.                          </td>  
  85.                     <td class="style7">  
  86.                          </td>  
  87.                 </tr>  
  88.                 <tr>  
  89.                     <td class="style1">  
  90.                         Username:</td>  
  91.                     <td class="style2">  
  92.                         <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"   
  93.                             ontextchanged="TextBox1_TextChanged"></asp:TextBox>  
  94.                             <asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender5" TargetControlID="TextBox1" WatermarkText="Enter Username" runat="server">  
  95.                         </asp:TextBoxWatermarkExtender>  
  96.                     </td>  
  97.                     <td class="style7">  
  98.                     <div id="checkusername" runat ="server" visible="false">  
  99.                         <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#FF5050"></asp:Label>  
  100.                         </div>  
  101.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"   
  102.                             ControlToValidate="TextBox1" ErrorMessage="Please Enter your Username"   
  103.                             ForeColor="#FF6600"></asp:RequiredFieldValidator>  
  104.                         </td>  
  105.                 </tr>  
  106.                 <tr>  
  107.                     <td class="style1">  
  108.                         <asp:Label ID="Label2" runat="server"  Text="word"></asp:Label>  
  109.                     </td>  
  110.                     <td class="style2">  
  111.                         <asp:TextBox ID="TextBox2" runat="server" TextMode="word"></asp:TextBox>  
  112.                     
  113.                     </td>  
  114.                     <td>  
  115.                     <%--<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"   
  116.                             ControlToValidate="TextBox2" ErrorMessage="Please Enter your word"   
  117.                             ForeColor="#FF6600"></asp:RequiredFieldValidator>--%>  
  118.                     </td>  
  119.                 </tr>  
  120.                 <tr>  
  121.                     <td class="style1">  
  122.                         <asp:Label ID="Label3" runat="server">ReTypeword</asp:Label>  
  123.                     </td>  
  124.                     <td class="style2">  
  125.                         <asp:TextBox ID="TextBox3" runat="server" TextMode="word"></asp:TextBox>       
  126.                     </td>  
  127.                     <td>  
  128.                     <asp:Label ID="Label6" runat="server"></asp:Label>  
  129.                     <%--<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"   
  130.                             ControlToValidate="TextBox3" ErrorMessage="Please Enter word Again"   
  131.                             ForeColor="#FF6600"></asp:RequiredFieldValidator>--%>  
  132.                        
  133.                     </td>  
  134.                 </tr>  
  135.                 <tr>  
  136.                     <td class="style1">  
  137.                         <asp:Label ID="Label4" runat="server" Text="Email"></asp:Label>  
  138.                     </td>  
  139.                     <td class="style2">  
  140.                         <asp:TextBox ID="TextBox4" runat="server">  
  141.              </asp:TextBox>  
  142.              <asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2" TargetControlID="TextBox4" WatermarkText="Enter EmailId." runat="server">  
  143.                         </asp:TextBoxWatermarkExtender>  
  144.                     </td>  
  145.                     <td>  
  146.                       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"   
  147.                             ControlToValidate="TextBox4" ErrorMessage="Please Enter your MailID"   
  148.                             ForeColor="#FF6600"></asp:RequiredFieldValidator>  
  149.                     </td>  
  150.                 </tr>  
  151.                 <tr>  
  152.                     <td class="style1">  
  153.                         <asp:Label ID="Label5" runat="server" Text="PhoneNo"></asp:Label>  
  154.                     </td>  
  155.                     <td class="style2">  
  156.                         <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>  
  157.                         <asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" TargetControlID="TextBox5" WatermarkText="Enter Phone No." runat="server">  
  158.                         </asp:TextBoxWatermarkExtender>  
  159.                     </td>  
  160.                       
  161.                    <td>  
  162.                      <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"   
  163.                            ControlToValidate="TextBox5" ErrorMessage="Please Enter your PhoneNo"   
  164.                            ForeColor="#FF6600"></asp:RequiredFieldValidator>  
  165.   
  166.                     </td>  
  167.                 </tr>  
  168.                 <tr>  
  169.                     <td class="style1">  
  170.                          </td>  
  171.                   <td class="style2">  
  172.                          </td>  
  173.                     <td class="style7">  
  174.                       
  175.                          </td>  
  176.                 </tr>  
  177.                 <tr>  
  178.                     <td class="style1">  
  179.                          </td>  
  180.                     <td class="style2">  
  181.                         <asp:Button ID="Button1" runat="server" BackColor="White" BorderColor="#99FF66"   
  182.                             BorderStyle="Dotted" BorderWidth="1px" ForeColor="#FF6600"   
  183.                             onclick="Button1_Click" Text="Register" />  
  184.                     </td>  
  185.                     <td>  
  186.   
  187.                 </td>  
  188.                 </tr>  
  189.             </table>   
  190.         </ContentTemplate>  
  191.          
  192.         </asp:UpdatePanel>  
  193.       
  194.     </div>  
  195.     </div>  
  196.     </form>  
  197. </body>  
  198. </html>  

Your design will look like the following:

design
Fire the event for the username TextBox, then click on Text_changed and by going to the username_textbox, press F4, go to the Properties window and open up events, then click on Textbox_changed.

Open up Events

Code Chamber

Step 5

We will add some code to the Reg_demo.aspx.cs page so that our Register form works correctly. 

Reg_demo.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class Default2 : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.   
  15.     }  
  16.     protected void TextBox1_TextChanged(object sender, EventArgs e)  
  17.     {  
  18.         if (!string.IsNullOrEmpty(TextBox1.Text))  
  19.         {  
  20.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  21.             SqlCommand cmd = new SqlCommand("select * from tbl_data where username=@username", con);  
  22.             cmd.Parameters.AddWithValue("username", TextBox1.Text);  
  23.             con.Open();  
  24.             SqlDataReader sdr = cmd.ExecuteReader();  
  25.             if (sdr.HasRows)  
  26.             {  
  27.                 checkusername.Visible = true;  
  28.                 Label1.Text = "Stop Here UserName Exist";  
  29.                 Label1.ForeColor = System.Drawing.Color.Red;  
  30.                 TextBox2.Visible = false;  
  31.                 TextBox3.Visible = false;  
  32.                 TextBox4.Visible = false;  
  33.                 TextBox5.Visible = false;  
  34.                 Button1.Visible = false;  
  35.                 Label2.Visible = false;  
  36.                 Label3.Visible = false;  
  37.                 Label4.Visible = false;  
  38.                 Label5.Visible = false;  
  39.             }  
  40.             else  
  41.             {  
  42.                 checkusername.Visible = true;  
  43.                 Label1.Text = "Please Proceed";  
  44.                 Label1.ForeColor = System.Drawing.Color.Green;  
  45.                 TextBox2.Visible = true;  
  46.                 TextBox3.Visible = true;  
  47.                 TextBox4.Visible = true;  
  48.                 TextBox5.Visible = true;  
  49.                 Button1.Visible = true;  
  50.                 Label2.Visible = true;  
  51.                 Label3.Visible = true;  
  52.                 Label4.Visible = true;  
  53.                 Label5.Visible = true;  
  54.             }  
  55.         }  
  56.         else  
  57.         {  
  58.             checkusername.Visible = false;  
  59.         }  
  60.     }  
  61.     protected void Button1_Click(object sender, EventArgs e)  
  62.     {  
  63.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  64.         SqlCommand cmd = new SqlCommand("sp_insert", con);  
  65.         cmd.CommandType = CommandType.StoredProcedure;  
  66.         cmd.Parameters.AddWithValue("username", TextBox1.Text);  
  67.         cmd.Parameters.AddWithValue("word", TextBox2.Text);  
  68.         cmd.Parameters.AddWithValue("email", TextBox4.Text);  
  69.         cmd.Parameters.AddWithValue("phoneno", TextBox5.Text);  
  70.         con.Open();  
  71.         cmd.ExecuteNonQuery();  
  72.         if (TextBox2.Text== TextBox3.Text)  
  73.         {  
  74.             Response.Redirect("Default.aspx");  
  75.         }  
  76.         else  
  77.         {  
  78.             Label6.Text = "word Dont Match";  
  79.         }  
  80.        con.Close();      
  81.     }     
  82. }  
In the Redirect page you can add anything, here we are adding a HTML website. Now, we are ready with the application and here is the output.

Output Chamber
output

Output Regform 1

Then click on the register button, the data is saved into the database as “Rinku”, he is the new user here and also the user will get redirected to the other page.

Output Regform 2

Output Regform 3

Now by entering this data again you can't proceed to the next step of registration since it will disable all the textboxes until you change the username.

registration form

I hope you liked this. Thank you for reading. Have a nice day!

Up Next
    Ebook Download
    View all
    Learn
    View all