How to check the username/email is exists or not in asp.net | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to check the username/email is exists or not in asp.net

Introduction:  In this article I will try to explain how we can check the Username/Email is Exists or not at the time of User registration in Asp.net.
username/email is exists or not

Description:
Here I created a table USER_REGISTRATION. USER_ID is primary key.
USER_ID
int
USERNAME
varchar(50)
FIRST_NAME
varchar(50)
LAST_NAME
varchar(50)
EMAIL
varchar(50)
PASSWORD
varchar(50)

Add a webform to project. Drag and drop the controls from Toolbox to .aspx page.


  <table border="1px solid">
    <tr><b>Check USERNAME/EMAIL is Exist or not<br />when User Register/SIGN UP to Website</b></tr>
    <tr><td>Username:</td><td>
        <asp:TextBox ID="txtusername" runat="server"></asp:TextBox></td></tr>
    <tr><td>First Name:</td><td>
        <asp:TextBox ID="txtfirst" runat="server"></asp:TextBox></td></tr>
    <tr><td>Last Name:</td><td>
        <asp:TextBox ID="txtlast" runat="server"></asp:TextBox></td></tr>
    <tr><td>Email:</td><td>
        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
    <tr><td>Password:</td><td>
        <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox></td></tr>
    <tr><td>Confirm Password:</td><td>
        <asp:TextBox ID="txtconfirm" runat="server" TextMode="Password"></asp:TextBox></td></tr>
    <tr><td>&nbsp;</td><td>
        <asp:Button ID="Button1" runat="server" Text="Register" OnClientClick="return ValidateEmail();"
            onclick="Button1_Click" /></td></tr>
    </table>

Add the below given Javascript between Head Tag to validate the Email Textbox:

<script language="javascript" type="text/javascript">
         function ValidateEmail() {
             var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
             var emailAddress = document.getElementById("<%= txtemail.ClientID %>").value;
             var valid = emailRegex.test(emailAddress);
             if (!valid) {
                 alert("Please Enter Valid Email address");
                 return false;
             } else
                 return true;
         }
  </script>

Now go to .aspx.cs page and write the below mention code:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Net;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsUsernameEmailExist())
        {
           
            Messagebox("Username/Email address already exists. Please try another");
            return;
        }
        if (txtpassword.Text == txtconfirm.Text)
        {
            SqlCommand cmd = new SqlCommand("insert into USER_REGISTRATION (USERNAME,FIRST_NAME,LAST_NAME,EMAIL,PASSWORD) values(@USERNAME,@FIRST_NAME,@LAST_NAME,@EMAIL,@PASSWORD)", con);
            cmd.Parameters.AddWithValue("@USERNAME", txtusername.Text);
            cmd.Parameters.AddWithValue("@FIRST_NAME", txtfirst.Text);
            cmd.Parameters.AddWithValue("@LAST_NAME", txtlast.Text);
            cmd.Parameters.AddWithValue("@EMAIL", txtemail.Text);
            cmd.Parameters.AddWithValue("@PASSWORD", txtpassword.Text);
            cmd.ExecuteNonQuery();
            Clear();
            Messagebox("User Register Successfully");
            cmd.Dispose();
      
        }
        else
        {
            Messagebox("Passowrd Not Match");
        }                 
    }
private bool IsUsernameEmailExist()
    {
        SqlCommand cmd = new SqlCommand("Select * from USER_REGISTRATION where USERNAME='" + txtusername.Text + "' or EMAIL='" + txtemail.Text + "'", con);
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";

        Page.Controls.Add(lblMessageBox);

    }
public void Clear()
    {
        txtusername.Text = "";
        txtfirst.Text = "";
        txtlast.Text = "";
        txtemail.Text = "";
        txtpassword.Text = "";
        txtconfirm.Text = "";
    }

In VB (.aspx.vb)

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Net.Mail

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ToString())

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If IsUsernameEmailExist() Then

            Messagebox("Username/Email address already exists. Please try another")
            Return
        End If
        If txtpassword.Text = txtconfirm.Text Then
            Dim cmd As New SqlCommand("insert into USER_REGISTRATION (USERNAME,FIRST_NAME,LAST_NAME,EMAIL,PASSWORD) values(@USERNAME,@FIRST_NAME,@LAST_NAME,@EMAIL,@PASSWORD)", con)
            cmd.Parameters.AddWithValue("@USERNAME", txtusername.Text)
            cmd.Parameters.AddWithValue("@FIRST_NAME", txtfirst.Text)
            cmd.Parameters.AddWithValue("@LAST_NAME", txtlast.Text)
            cmd.Parameters.AddWithValue("@EMAIL", txtemail.Text)
            cmd.Parameters.AddWithValue("@PASSWORD", txtpassword.Text)
            cmd.ExecuteNonQuery()
            Clear()
            Messagebox("User Register Successfully")
            cmd.Dispose()
        Else
            Messagebox("Passowrd Not Match")
        End If
    End Sub
    Private Function IsUsernameEmailExist() As Boolean
        Dim cmd As New SqlCommand(("Select * from USER_REGISTRATION where USERNAME='" + txtusername.Text & "' or EMAIL='") + txtemail.Text & "'", con)
        cmd.ExecuteNonQuery()
        Dim dt As New DataTable()
        Dim adp As New SqlDataAdapter(cmd)
        adp.Fill(dt)
        If dt.Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub Messagebox(ByVal Message As String)
        Dim lblMessageBox As New Label()

        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"

        Page.Controls.Add(lblMessageBox)

    End Sub
Public Sub Clear()
        txtusername.Text = ""
        txtfirst.Text = ""
        txtlast.Text = ""
        txtemail.Text = ""
        txtpassword.Text = ""
        txtconfirm.Text = ""
    End Sub

Now run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget