dotnetwarriors.com

How to make simple Registration Form in Asp.Net using C#.Net

Aug 06, 2018 Asp.Net C# Sql Server 154
In this article i will explain how to make a simple registration form in asp.net using c#.net and Sql server with proper asp.net form validation.

first of all create a table users in sql server for inserting the users registration data as shown in below image:

 

How to make simple Registration Form in Asp.Net using C#.Net

after creating the above table, i will create a new web project in Visual Studio by navigating

File >New >Project >

and Select Asp.Net Empty Web Application

 

Now add a new Empty Web Page in project from solution explorer by right click on project name and navigate to add >New Item >Web form (As below image):

 

How to make simple Registration Form in Asp.Net using C#.Net

 

Write the below code in .aspx page to design the web form as shown below image:

How to make simple Registration Form in Asp.Net using C#.Net

.Aspx Code

 

<head runat="server">
    <title>simple registration form</title>
    <style>
        b
        {
            width:200px;
            display:inline-block;
            text-align:right;
        }
        input
        {
            width:200px;
            margin-left:20px;
        }
        div
        {
            margin-bottom:5px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server"><br />
        <div><h1>Simple Registration Form</h1></div>
       
         <div><b>First Name: </b><asp:TextBox ID="name1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Required1" runat="server" ErrorMessage="Enter first name" ControlToValidate="name1"></asp:RequiredFieldValidator></div>
        <div><b>Last Name: </b><asp:TextBox ID="name2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Required2" runat="server" ErrorMessage="Enter last name" ControlToValidate="name2"></asp:RequiredFieldValidator></div>
        
        <div><b>Email Name: </b><asp:TextBox ID="email1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Required3" runat="server" ErrorMessage="Enter Email id" ControlToValidate="email1"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RE1" runat="server" ErrorMessage="Enter correct email id" ControlToValidate="email1" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></div>
       
         <div><b>Password: </b><asp:TextBox ID="pass1" runat="server" TextMode="Password"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Required4" runat="server" ErrorMessage="Enter Password" ControlToValidate="pass1"></asp:RequiredFieldValidator></div>
        
        <div><b>Confirm Password: </b><asp:TextBox ID="repass" runat="server" TextMode="Password"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Required5" runat="server" ErrorMessage="Re enter password" ControlToValidate="repass"></asp:RequiredFieldValidator>
            <asp:CompareValidator ID="Compare1" runat="server" ErrorMessage="Password do not match" ControlToCompare="pass1" ControlToValidate="repass"></asp:CompareValidator></div>
       
         <div><b></b><asp:Button ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" /></div>
    </form>
    </body>

 

and after that write the below code in .aspx.cs file :

 

SqlConnection con;
        protected void Page_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=hello\\SQLEXPRESS;Initial Catalog=aspdotnetcorner;Integrated Security=True");
        }
        protected void Button1_Click(object sender, EventArgs e) //submit button click event
        {
            string str1 = "insert into users values('" + name1.Text + "','" + name2.Text + "','" + email1.Text + "','" + pass1.Text + "')";  //sql command text string
            SqlCommand cmd = new SqlCommand(str1,con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Registered Successfully...');", true);
        }

 

After saving these files Run the code with Ctrl+F5 or F5

How to make simple Registration Form in Asp.Net using C#.Net

if you try to submit form without proper entry then form display the validation Error as below image:

How to make simple Registration Form in Asp.Net using C#.Net

After Submit the proper form a alert box will be display with Success Message.

 

How to make simple Registration Form in Asp.Net using C#.Net

 





Share This Article :

Comments


Add Comment


 
Security Code :
9 + 10 =