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:
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 -
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.
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.
Html code of form:
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.
You will need following software's to accomplish this:
- Visual Studio 2010
- 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 -
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>        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">  
                                 
           <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />  
                      
           <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
Post a Comment