Check UserName Email Availability In Asp.Net Ajax

This example explains how to Check Email Or UserName Availability Using Ajax In Asp.Net C# VB.

check username email availability using ajax in asp.net
You may also like to read one of my previous posts where i described how to Create User Registration Form.

For this create a table Users in sql server database with ID,Uname,emlAddress columns and add some records in it.

Add ScriptManager,Ajax UpdatePanel on the page, and inside ContentTemplate place two textbox, two image control to display images and two label controls for related messages.

Set AutoPostBack property of textbox to true.

HTML SOURCE OF PAGE
   1:  <asp:ScriptManager ID="ScriptManager1" runat="server"/>
   2:  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   3:  <ContentTemplate>
   4:      UserName: 
   5:      <asp:TextBox ID="txtUName" runat="server" 
   6:                   ontextchanged="txtUName_TextChanged" 
   7:                   AutoPostBack="True"/>
   8:      
   9:      <asp:Image ID="imgUsr" runat="server" Visible="false"/>
  10:      <asp:Label ID="lblUsr" runat="server"/>
  11:      
  12:      Email ID:
  13:      <asp:TextBox ID="txtId" runat="server" 
  14:                   AutoPostBack="True" 
  15:                   ontextchanged="txtId_TextChanged"/>
  16:      <asp:Image ID="imgId" runat="server" Visible="false"/>
  17:      <asp:Label ID="lblId" runat="server"></asp:Label>
  18:  </ContentTemplate>
  19:  </asp:UpdatePanel>

Write below mentioned code in TextChanged Event of textbox

C# CODE
protected void txtUName_TextChanged(object sender, EventArgs e)
    {
        if (txtUName.Text != string.Empty)
        {
            string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            string strSelect = "SELECT COUNT(*) FROM Users WHERE Uname = @Username";
            SqlConnection con = new SqlConnection(strConnection);
            SqlCommand cmd = new SqlCommand(strSelect,con);

            SqlParameter user = new SqlParameter("@Username", SqlDbType.VarChar);
            user.Value = txtUName.Text.Trim().ToString();
            cmd.Parameters.Add(user);
            con.Open();
            int result = (Int32)cmd.ExecuteScalar();
            con.Close();

            if (result >= 1)
            {
                imgUsr.ImageUrl = "unavailable.png";
                imgUsr.Visible = true;
                lblUsr.Text = "Username not available";
                lblUsr.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                imgUsr.ImageUrl = "tick.png";
                imgUsr.Visible = true;
                lblUsr.Text = "Available";
                lblUsr.ForeColor = System.Drawing.Color.Green;
            }
        }

    }

VB.NET CODE
Protected Sub txtUName_TextChanged(sender As Object, e As EventArgs)
 If txtUName.Text <> String.Empty Then
  Dim strConnection As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  Dim strSelect As String = "SELECT COUNT(*) FROM Users WHERE Uname = @Username"
  Dim con As New SqlConnection(strConnection)
  Dim cmd As New SqlCommand(strSelect, con)

  Dim user As New SqlParameter("@Username", SqlDbType.VarChar)
  user.Value = txtUName.Text.Trim().ToString()
  cmd.Parameters.Add(user)
  con.Open()
  Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32)
  con.Close()

  If result >= 1 Then
   imgUsr.ImageUrl = "unavailable.png"
   imgUsr.Visible = True
   lblUsr.Text = "Username not available"
   lblUsr.ForeColor = System.Drawing.Color.Red
  Else
   imgUsr.ImageUrl = "tick.png"
   imgUsr.Visible = True
   lblUsr.Text = "Available"
   lblUsr.ForeColor = System.Drawing.Color.Green
  End If
 End If

End Sub

Similarly we can check email availability by writing following code

protected void txtId_TextChanged(object sender, EventArgs e)
    {
        if (txtId.Text != string.Empty)
        {
            string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            string strSelect = "SELECT COUNT(*) FROM Users WHERE emlAddress = @Email";
            SqlConnection con = new SqlConnection(strConnection);
            SqlCommand cmd = new SqlCommand(strSelect, con);
            cmd.Parameters.AddWithValue("@Email", txtId.Text.Trim().ToString());

            con.Open();
            int result = (Int32)cmd.ExecuteScalar();
            con.Close();

            if (result >= 1)
            {
                imgId.ImageUrl = "unavailable.png";
                imgId.Visible = true;
                lblId.Text = "Email already registered";
                lblId.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                imgId.ImageUrl = "tick.png";
                imgId.Visible = true;
                lblId.Text = "Available";
                lblId.ForeColor = System.Drawing.Color.Green;
            }
        }
    }


Download Sample Code


If you like this post than join us or share

5 comments:

Anonymous said...

Dear Sir

I have created connection string in web.config file. Is there any problem when i run check availabiltiy in complex registration page like

FirstName
LastName
Email ID
UserName
Password

Thank you


Unknown said...

@Above: What error you are getting ?

refer link to know how to Create User Registration Page


Anonymous said...

Thanku sir ..it help me more in my project ..thank u very much..


Anonymous said...

can u explain this into 3 tier archtct example


Anonymous said...

IT WORKS WELL


Find More Articles