SIMPLE REGISTRATION FORM IN ASP.NET WITH C#.NET and VB.NET
In this article we will see a simple registration form in asp.net with all client side validations. I am designing a registration form with fields like first name, last name, email, etc,.. and inserting the data into database using insert sql statement .
Here I am adding a local database to my asp.net application and using the local database. The connection string is taken in web.config file. You can download the source code from the link provided below.
Create table in sql server
I am designing the aspx page as follows
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserRegistration.aspx.cs" Inherits="_Default" %> <!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"> .style4 { width: 212px; } .style7 { width: 212px; height: 31px; } .style9 { height: 26px; } .style11 { width: 259px; } .style12 { width: 259px; height: 31px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table style="border: 1pt solid #6666FF; width: 60%; height: 424px; font-family: Verdana; border-collapse: collapse; background-color: #ffffff;" align="center"> <tr> <td align="center" colspan="3" class="style9"> <asp:Label ID="lblHeader" runat="server" Text="Registration Form" Font-Bold="True"></asp:Label> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblFirstName" runat="server" Text="First Name :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvFirstName" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtFirstName"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblLastName" runat="server" Text="Last Name :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvLastName" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtLastName"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblEmail" runat="server" Text="E-Mail :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvUserName" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtEmail"></asp:RequiredFieldValidator> <br /> <asp:RegularExpressionValidator ID="RxvEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid E-mail" ForeColor="#FF3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblPassword" runat="server" Text="Password :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvPwd" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtPassword"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblConfirmPwd" runat="server" Text="Confirm Pasword :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvCnfrmPwd" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtConfirmPwd"></asp:RequiredFieldValidator> <br /> <asp:CompareValidator ID="CvCnfmPwd" runat="server" ErrorMessage="Password and Confirm Password didnt matched" ForeColor="#FF3300" ControlToCompare="txtPassword" ControlToValidate="txtConfirmPwd"></asp:CompareValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblGender" runat="server" Text="Gender :"></asp:Label> </td> <td class="style11" align="left"> <asp:RadioButtonList ID="RdoGender" runat="server" RepeatDirection="Horizontal" Width="176px"> <asp:ListItem>Male</asp:ListItem> <asp:ListItem>Female</asp:ListItem> </asp:RadioButtonList> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvGender" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="RdoGender"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblDob" runat="server" Text="Date of Birth :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtDob" runat="server"></asp:TextBox> <asp:Label ID="Label1" runat="server" Text="(dd/mm/yyyy)"></asp:Label> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required" ForeColor="#FF3300" ControlToValidate="txtDob"></asp:RequiredFieldValidator> <br /> <asp:RegularExpressionValidator ID="RxvDob" runat="server" ControlToValidate="txtDob" ErrorMessage="Invalid Date Format" ValidationExpression="^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$" ForeColor="#FF3300"></asp:RegularExpressionValidator> </td> </tr> <tr> <td align="right" class="style11"> <asp:Label ID="lblMobile" runat="server" Text="Mobile"></asp:Label> </td> <td class="style11"> <asp:TextBox ID="txtMobile" runat="server" MaxLength="10"></asp:TextBox> </td> <td class="style4"> <asp:RegularExpressionValidator ID="RxvMobile" runat="server" ErrorMessage="Invalid Mobile Number" ForeColor="#FF3300" ValidationExpression="^([7-9]{1})([0-9]{9})$" ControlToValidate="txtMobile"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style11" align="right"> <asp:Label ID="lblAddress" runat="server" Text="Address :"></asp:Label> </td> <td class="style11" align="left"> <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox> </td> <td class="style4" align="left"> <asp:RequiredFieldValidator ID="RfvAddress" runat="server" ErrorMessage="* Required" ForeColor="Red" ControlToValidate="txtAddress"></asp:RequiredFieldValidator> </td> </tr> <td align="center" class="style12"> <asp:Label ID="lblMsg" runat="server" ForeColor="#CC3300"></asp:Label> </td> <td class="style12"> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> <asp:Button ID="btnClear" runat="server" CausesValidation="False" OnClick="btnClear_Click" Text="Clear" /> </td> <td align="center" class="style7"> </td> </tr> </table> </div> </form> </body> </html>
The c# code in the code behind file is given below :
using System; using System.Web.UI; using System.Data.SqlClient; using System.Configuration; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //declaring connection string and command //here we are extracting connection string from web.config file SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString()); SqlCommand cmd = new SqlCommand(); protected void btnSubmit_Click(object sender, EventArgs e) { try { cmd.Connection = con; //assigning connection to command cmd.CommandType = CommandType.Text; //representing type of command //cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values // (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)"; cmd.CommandText = "INSERT INTO UserDetails values(@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)"; //adding parameters with value cmd.Parameters.AddWithValue("@Fname", txtFirstName.Text.ToString()); cmd.Parameters.AddWithValue("@Lname", txtLastName.Text.ToString()); cmd.Parameters.AddWithValue("@Email",txtEmail.Text.ToString()); cmd.Parameters.AddWithValue("@Password",txtPassword.Text.ToString()); cmd.Parameters.AddWithValue("@Gender",RdoGender.SelectedItem.Text.ToString()); cmd.Parameters.AddWithValue("@Dob",txtDob.Text.ToString()); cmd.Parameters.AddWithValue("@Mobile",txtMobile.Text.ToString()); cmd.Parameters.AddWithValue("@Address",txtAddress.Text.ToString()); con.Open(); //opening connection cmd.ExecuteNonQuery(); //executing query con.Close(); //closing connection lblMsg.Text="Registered Successfully.."; } catch(Exception ex) { lblMsg.Text=ex.Message.ToString(); } } protected void btnClear_Click(object sender, EventArgs e) { //refreshing/reloading page to clear all the controls Page.Response.Redirect(Page.Request.Url.ToString(), true); } }
The VB.net code in the code behind file is given below :
Imports System.Web.UI Imports System.Data.SqlClient Imports System.Configuration Imports System.Data Partial Class Registration_Vb Inherits System.Web.UI.Page 'declaring connection string and command 'here we are extracting connection string from web.config file Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ToString()) Private cmd As New SqlCommand() Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Try cmd.Connection = con 'assigning connection to command cmd.CommandType = CommandType.Text 'representing type of command 'cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values ' (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)"; cmd.CommandText = "INSERT INTO UserDetails values(@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)" 'adding parameters with value cmd.Parameters.AddWithValue("@Fname", txtFirstName.Text.ToString()) cmd.Parameters.AddWithValue("@Lname", txtLastName.Text.ToString()) cmd.Parameters.AddWithValue("@Email", txtEmail.Text.ToString()) cmd.Parameters.AddWithValue("@Password", txtPassword.Text.ToString()) cmd.Parameters.AddWithValue("@Gender", RdoGender.SelectedItem.Text.ToString()) cmd.Parameters.AddWithValue("@Dob", txtDob.Text.ToString()) cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString()) cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString()) con.Open() 'opening connection cmd.ExecuteNonQuery() 'executing query con.Close() 'closing connection lblMsg.Text = "Registered Successfully.." Catch ex As Exception lblMsg.Text = ex.Message.ToString() End Try End Sub Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click 'refreshing/reloading page to clear all the controls Page.Response.Redirect(Page.Request.Url.ToString(), True) End Sub End Class
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.