ASP.NET WebForms - Creating A Simple Registration Form

This example shows, how to create a very simple registration form in ASP.NET WebForms, using the traditional ADO.NET. Here’s, the glimpse of the page output:
output
Creating the Database

The following are the basic steps on how to create a simple database in SQL Server:

  1. Launch SQL Server Management Studio Express and then connect.
  2. Expand Databases folder from SQL Server object Explorer.
  3. Right click Databases folder and select “New Database”.
  4. From the pop-up Window, input the database name, which you would like and then click add.
  5. Expand Database folder, which you have just added.
  6. Right click on the Tables folder and select “New Table”.
  7. Add the following fields, given below:

    table

Note: In this demo, we will set the Id to auto increment, so that the Id will be automatically generated for every new added row. To do this, select the Column name “Id” and in the column properties, set the “Identity Specification” to yes.

After adding all the necessary fields, name your database table to whatever you like. For this particular demo, we will just name it as “tblRegistration”.

Setting up the UI

For the simplicity of this demo, we will set up UI like:
output
Here’s, the equivalent ASPX Markup:

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title>Sample Registration Page</title>  
  5.     <style type="text/css">  
  6.         .style1 {  
  7.             width: 100%;  
  8.         }  
  9.     </style>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.         <div>  
  14.             <table class="style1">  
  15.                 <tr>  
  16.                     <td>Full Name:</td>  
  17.                     <td>  
  18.                         <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>  
  19.                     </td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>Username:</td>  
  23.                     <td>  
  24.                         <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>  
  25.                     </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td>Password:</td>  
  29.                     <td>  
  30.                         <asp:TextBox ID="TxtPassword" runat="server"  
  31.                                      TextMode="Password"></asp:TextBox>  
  32.                     </td>  
  33.                 </tr>  
  34.                 <tr>  
  35.                     <td>Re Password:</td>  
  36.                     <td>  
  37.                         <asp:TextBox ID="TxtRePassword" runat="server"  
  38.                                      TextMode="Password"></asp:TextBox>  
  39.                     </td>  
  40.                 </tr>  
  41.                 <tr>  
  42.                     <td>Address:</td>  
  43.                     <td>  
  44.                         <asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox>  
  45.                     </td>  
  46.                 </tr>  
  47.                 <tr>  
  48.                     <td>Age:</td>  
  49.                     <td>  
  50.                         <asp:TextBox ID="TxtAge" runat="server"></asp:TextBox>  
  51.                     </td>  
  52.                 </tr>  
  53.                 <tr>  
  54.                     <td>Gender:</td>  
  55.                     <td>  
  56.                         <asp:DropDownList ID="DropDownList1" runat="server"  
  57.                                           AppendDataBoundItems="true">  
  58.                             <asp:ListItem Value="-1">Select</asp:ListItem>  
  59.                             <asp:ListItem>Male</asp:ListItem>  
  60.                             <asp:ListItem>Female</asp:ListItem>  
  61.                         </asp:DropDownList>  
  62.                     </td>  
  63.                 </tr>  
  64.             </table>  
  65.         </div>  
  66.         <asp:Button ID="Button1" runat="server" Text="Save"  
  67.                     onclick="Button1_Click" />  
  68.     </form>  
  69. </body>  
  70. </html>  
There’s nothing really fancy there. The markup, shown above, just contains some basic Server controls to compose the page.

Setting up the ConnectionString

Now, open the web.config file and configure the connection string, as shown below:
  1. <connectionStrings>  
  2.     <add name="DBConnectionString" connectionString="Data Source=YourDatabaseServerName;  
  3.                              Initial Catalog=YourDatabase;  
  4.                              Integrated Security=SSPI;"  
  5.          providerName="System.Data.SqlClient" />  
  6. </connectionStrings>  
Few things to note there: The “DBConnectionString” is the value, which we are going to use for referencing the connection string, we defined above. You need to change the value of Data Source and Initial Catalog, based on your Server name and database name.

The Member Class

Now, let’s create a new class, which would hold the following properties:
  1. public class Member{  
  2.         public string Name { getset; }  
  3.         public string UserName { getset; }  
  4.         public string Password { getset; }  
  5.         public string Gender { getset; }  
  6.         public string Age { getset; }  
  7.         public string Address { getset; }  
  8. }  
The Code Behind

Switch to your code behind the file and add the following method to get the connection string:
  1. public string GetConnectionString()  
  2. {  
  3.     return ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;  
  4. }  
We will be using ADO.NET objects to handle an insert operation to the database. If you are not familiar with ADO.NET, I would suggest you  refer to the link, given below, to get started:

ADO.NET Tutorial

Here’s, the code block to insert the data to the database:
  1. private void ExecuteInsert(Member member) {  
  2.             using (SqlConnection sqlConn = new SqlConnection(GetConnectionString()))  
  3.             {  
  4.                 string sql = @"INSERT INTO tblRegistration   
  5.                              (Name, UserName, Password, Gender, Age, Address)   
  6.                               VALUES  
  7.                              (@Name,@UserName,@Password,@Gender,@Age,@Address)";  
  8.   
  9.                 using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))  
  10.                 {  
  11.                     sqlCmd.Parameters.AddWithValue("@Name", member.Name);  
  12.                     sqlCmd.Parameters.AddWithValue("@UserName", member.UserName);  
  13.                     sqlCmd.Parameters.AddWithValue("@Password", member.Password);  
  14.                     sqlCmd.Parameters.AddWithValue("@Gender", member.Gender);  
  15.                     sqlCmd.Parameters.AddWithValue("@Age", member.Age);  
  16.                     sqlCmd.Parameters.AddWithValue("@Address", member.Address);  
  17.   
  18.                     sqlConn.Open();  
  19.                     sqlCmd.CommandType = CommandType.Text;  
  20.                     sqlCmd.ExecuteNonQuery();  
  21.                 }  
  22.             }   
  23.         }  
The code, shown above, is the typical way of adding new data to the database, using ADO.NET. Notice, we pass the Member class as the parameter to the method. The Member class holds the data collected from the form. We’ll see this in the next step.

Now, let’s call the method, shown above, at button’s click event like:
  1. protected void Button1_Click(object sender, EventArgs e) {  
  2.             if (TxtPassword.Text == TxtRePassword.Text)  
  3.             {  
  4.                 //fill the class properties with form values  
  5.                 Member member = new Member();  
  6.                 member.Name = TxtName.Text;  
  7.                 member.UserName = TxtUserName.Text;  
  8.                 member.Password = TxtPassword.Text;  
  9.                 member.Gender = DropDownList1.SelectedItem.Text;  
  10.                 member.Age = TxtAge.Text;  
  11.                 member.Address = TxtAddress.Text;  
  12.                 //call the method to execute insert to the database  
  13.                 ExecuteInsert(member);  
  14.                 Response.Write("Record was successfully added!");  
  15.                 ClearControls(Page);  
  16.             }  
  17.             else  
  18.             {  
  19.                 Response.Write("Password did not match");  
  20.                 TxtPassword.Focus();  
  21.             }  
  22.  }  
The code, shown above, checks if the password matches with the re-type password field. If it matches, it will call the method ExcuteInsert(), else, it will display an error message. You may have noticed the call to ClearControls() method from the code, shown above. Here’s the code for it:
  1. private static void ClearControls(Control Parent)  
  2.             if (Parent is TextBox)  
  3.             { (Parent as TextBox).Text = string.Empty; }  
  4.             else  
  5.             {  
  6.                 foreach (Control c in Parent.Controls)  
  7.                     ClearControls(c);  
  8.             }  
  9. }  
It's simple.

 


Similar Articles