Skip to main content

Create a simple registration form in ASP.Net with C#

In this article we will how to create or design a simple registration form in ASP.Net with the help of C sharp and store the entries in SQL Server database.


You will need following software's to accomplish this:
  1. Visual Studio 2010
  2. SQL Server 2008

Follow the steps below to make it:

Step 1: First open your visual studio 2010 -> File -> New -> Web site -> ASP.NET Empty Web site -> click OK. (Make sure to select Visual C# from installed template and you can give the name to the website.)

Step 2: Now in Solution Explorer -> Add New Item -> Web Form -> click on Add.

Step 3: Now make the basic html table in the source section of empty form and insert the fields like - 


registration form in ASP.Net with C sharp

Use the Text boxes for each field, Label for username to see the availability, Buttons from the Toolbox section. Set unique ID for these tools to avoid confusion.

To make the reset button add following line in the source code near the submit button.
 <input id="Reset1" type="reset" value="Reset" onclick="return Reset1_onclick()" />  

Step 4: To setup SQL Server connectivity Go to -> Server explorer -> Right click on Data Connections -> Add connection -> Select Data Source Microsoft SQL Server(SqlClient) -> Select server name -> Select or enter the name of database -> Test the connection -> Click Ok.
 (To select the data source Go to -> Server explorer -> Right click on newly added database -> Properties -> Copy the connection string)

Step 5: Now double click on Submit button to write C# code and copy paste the following code.

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data.Sql;  
 using System.Data.SqlClient;  
 using System.Drawing;  
 public partial class registration : System.Web.UI.Page  
 {  
   protected void Page_Load(object sender, EventArgs e)  
   {  
   }  
   SqlConnection connection = new SqlConnection("Data Source=*;Initial Catalog=PlacementCellProject;Integrated Security=True");  //Copy paste your connection string from newly created database.
   SqlCommand cmd;  
   SqlDataReader dr;  
   protected void SubmitButton_Click(object sender, EventArgs e)  
   {  
     connection.Open();  
     cmd = new SqlCommand("insert into StudentRegistration values('"+txtFname.Text+"', '"+txtMname.Text+"', '"+txtLname.Text+"', '"+txtUname.Text+"', '"+txtEnno.Text+"', '"+txtEmail.Text+"', '"+txtPassword.Text+"', '"+txtRepassword.Text+"', '"+txtMob.Text+"')", connection);  
     cmd.Parameters.AddWithValue("@Username", txtUname.Text);  
     dr = cmd.ExecuteReader();  
     if (dr.HasRows)  
     {  
       MsgLabel.Text = "Username is already exist!";  
       this.MsgLabel.ForeColor = Color.Red;  
     }  
     else  
     {  
       MsgLabel.Text = "Username is available!";  
       this.MsgLabel.ForeColor = Color.Green;  
     }  
   }  
 }  

Html code of form:
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
   <style type="text/css">  
     .style1  
     {  
       font-size: xx-large;  
       color: #FF0000;  
     }  
   </style>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <table>  
       <tr>  
         <td colspan="3" class="style1">  
           <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enter Details </strong>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>First Name*</strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Middle Name* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtMname" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Last Name* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Username* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtUname" runat="server"></asp:TextBox>  
         </td>  
         <td>  
           <asp:Label ID="MsgLabel" runat="server"></asp:Label>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Enrollment No.*</strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtEnno" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Email* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Password* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Re-type Password* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtRepassword" runat="server" TextMode="Password"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td>  
           <strong>Mobile No* </strong>  
         </td>  
         <td>  
           <asp:TextBox ID="txtMob" runat="server"></asp:TextBox>  
         </td>  
         <td>  
         </td>  
       </tr>  
       <tr>  
         <td colspan="3">  
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
           <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />  
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
           <input id="Reset1" type="reset" value="Reset" onclick="return Reset1_onclick()" />  
         </td>  
       </tr>  
     </table>  
   </div>  
   </form>  
 </body>  
 </html>  

Step 6: Now hit on debug button to test the code. To check whether the entries has been successfully stored or not check the table of your database in SQL Server.


I hope this tutorial helpful for you! If you have any difficulty regarding this post your queries below in comment section. :)

Comments

Popular posts from this blog

How to use XBOX 360 Controller Emulator (x360ce) for NFS Rivals and other PC Games

A s we know many of PC Games only supports XBOX 360 controller officially to play the game. If you have local pc gamepad controller then it might not work well with some pc games. So you have to use application called x360ce i.e XBOX 360 Controller Emulator. It emulate your non xbox 360 controller i.e gamepad as xbox 360 controller i.e. force non xbox 360 controller to work as xbox 360 controller. So in this article we will see how to configure x360ce to work with NFS Rivals. HOW TO USE XBOX 360 CONTROLLER EMULATOR (x360ce) 1) Download x360ce and extract xbox360ce.exe in folder where you have installed NFS Rivals. 2) Now open the x360ce.exe from NFS Rivals folder. 3) You will see message like-  'x360ce.ini' was not found. Click on Yes to create ini file. 4) After that you will see the message- 'xinput1_3.dll' was not found. Click on Yes to create it. 5) Next it will detect your controller device. Now select 'Browse my computer for settings...

How to change blogger Time Zone? Set your country time zone.

C hanging the time zone may be not major thing to do but it is important to set correct time zone for some bloggers. If you want schedule your blog post in future then you may face difficulty to set date and time. If you set different time zone than your native timezone you will not able schedule correct time & date to publish your post. Let's look how to set correct time zone for your country. First login into your blogger a/c and goto your blog. Then goto Settings → Language and formatting. In the formatting section you will see the Time Zone change it to your native time zone. For example, I have set it to my native zone that is GMT +5:30 India Standard Time Zone. T his may be minor blogger tip & trick for you. But for some people specially newbies it could be difficult to find. Please leave your positive feedback below in the comment section to appreciate this post.Thank you!

How to download Scribd books in PDF for free

You guys have definitely heard about Scribd. It is a digital library, featuring an electronic book monthly subscription service. They have different monthly plans for reading and downloading an electronic book in various formats such as PDF, DOC, TXT, etc. If you have credit card or Paypal account then you can easily get monthly subscription. You have to pay once for a month, then you can read and download any book in that duration after that you have to extend your subscription. If you don't have a credit card and want to download ebook then we are going to see how to do it for free. HOW TO DOWNLOAD SCRIBD BOOKS FOR FREE 1. Go to Scribd. 2. Search or Brows the electronic book that you want to download. 3. Now copy the URL of that electronic book. Now click on Download. 4. You can login with your Facebook account or you can sign up for free. I'm going to login Scribd with my facebook account. 5. Now you will be redirected to monthly subscription page...