Thursday, 4 December 2014

Validation of Duplicate Email Id Of Registration Form Using C# in Asp.Net

12/04/2014 - By Pranav Singh 1


This article will show you how you can validate duplicate email id present in an sql table using c# in asp.net. In this we have shown using registration form.


First we will create a new new table in sql DB.




Query to create table.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserTable](
      [Id] [int] NULL,
      [EmailId] [varchar](50) NULL,
      [Name] [varchar](50) NULL,
      [Password] [varchar](50) NULL,
      [ContactNo] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

So for this article first we will create a new asp.net application and add the below code into your asp.net page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>Validation of Duplicate Email Id Of Registration Form Using C# in Asp..Net </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="right" class="style1" colspan="2" style="text-align: center">
                <strong>REGISTRATION FORM</strong>
            </td>
        </tr>
        <tr>
            <td align="right">
                Email Id :
            </td>
            <td align="left">
                <asp:TextBox ID="txtemail" runat="server" title="Enter your email id"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtemail"
                    ErrorMessage="*"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtemail"
                    ErrorMessage="*" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="right">
                Password :
            </td>
            <td align="left">
                <asp:TextBox ID="txtpassword" runat="server" title="Enter your password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtpassword"
                    ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right">
                Confirm Password :
            </td>
            <td align="left">
                <asp:TextBox ID="txtconfpassword" runat="server" title="Enter confirm password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtconfpassword"
                    ErrorMessage="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtpassword"
                    ControlToValidate="txtconfpassword" ErrorMessage="*"></asp:CompareValidator>
            </td>
        </tr>
        <tr>
            <td align="right">
                Name :
            </td>
            <td align="left">
                <asp:TextBox ID="txtname" runat="server" title="Enter your name"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtname"
                    ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right">
                Contact No :
            </td>
            <td align="left">
                <asp:TextBox ID="txtcontact" runat="server" title="Enter your contact no"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtcontact"
                    ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right">
                 
            </td>
            <td align="left">
                <asp:Label ID="lblmessage" runat="server" Style="color: #FF6666; font-weight: 700;"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Now generate the button click event and add the below code into the button click.

using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            /* In this you just make the email id unik field
             * so when you add duplicate email id it will give
             * exception
             */
            try
            {
                SqlConnection con = new SqlConnection("Data Source=PC\\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
                string queryValidate = "Select * from UserTable where EmailId='" + txtemail.Text + "'";
                DataTable objdt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(queryValidate, con);
                da.Fill(objdt);
                if (objdt.Rows.Count > 0)
                {
                    lblmessage.Text = "Email Id Already Exists.";
                }
                else
                {
                    string query = "Insert into UserMaster(EmailId,Password,Name,ContactNo) values('" + txtemail.Text + "','" + txtpassword.Text + "','" + txtname.Text + "','" + txtcontact.Text + "');";
                    SqlDataAdapter saveda = new SqlDataAdapter(query, con);
                    saveda.SelectCommand.ExecuteNonQuery();
                    lblmessage.Text = "User data saved successfully.";
                }
            }
            catch (Exception ex)
            {
                //Your error message
            }
        }
    }
}

In above code I have used select query to check weather the email id present in sql table or not. If we found any record after eecuting the query on that case email id exists in table.

Now run the application and check the output.


Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top