Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to check User Name and Email availability on Register Page in ASP.NET

Author: Rachel Nicole
Download Source Code : 1431_CheckUserNameEmailAvailability.zip

The purpose of this article is to learn how we can check user name and email availability in register page using AJAX in ASP.NET.

 

Almost every web site provides registration facility to users to become member on the website. This membership can be for any purpose and with any type of information. One thing is sure in the website that membership providers do not want a users registered for more than one time with same email or same user name. For this purpose, they must have to check user name or email availability at registration time. This requirement can be accomplished with different techniques. I will write a function to check user name and email availability and use AJAX to show it on the page.
 
I have created a Sample database and a table with the name if Users in the database for this article. Users table has only three columns UserId, UserName and Email. I have also added few test records into the table.
 
  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C# > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Write code below in Default.aspx page

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                  <ContentTemplate>
                         <table>
                               <tr>
                                      <td>User Name:</td>
                                      <td>
                                             <asp:TextBox ID="txtBoxUserName" runat="server">
                                             </asp:TextBox>
                                      </td>
                                      <td>
                                             <asp:Image ID="imgUserName" Width="15" Height="15" runat="server" />
                                             <asp:Label ID="lblUserName" runat="server"></asp:Label>
                                      </td>
                               </tr>
                               <tr>
                                      <td>Email:</td>
                                      <td>
                                             <asp:TextBox ID="txtBoxEmail" runat="server">
                                             </asp:TextBox>
                                      </td>
                                      <td>
                                             <asp:Image ID="imgEmail" Width="15" Height="15" runat="server" />
                                             <asp:Label ID="lblEmail" runat="server"></asp:Label>
                                      </td>
                               </tr>
                               <tr align="center">
                                      <td colspan="3">
                                             <asp:Button ID="btnRegister" runat="server" Text="Register" />
                                      </td>
                         </tr>
                  </table>s
           </ContentTemplate>
    </asp:UpdatePanel>

    To achieve AJAX functionality, we need to add a ScriptManager control and an UpdatePanel control to the page.

     
  6. Write two stored procedures to check User Name and Email availability in the database.

    CREATE PROCEDURE dbo.UserNameAvailability
           (
           @UserName nvarchar(100),
           @UserNameCount int OUTPUT
           )
    AS
                         SELECT @UserNameCount = Count(*) FROM Users WHERE UserName = @UserName
     
                         RETURN @UserNameCount
     
     
    CREATE PROCEDURE dbo.EmailAvailability
           (
           @Email nvarchar(200),
           @EmailCount int OUTPUT
           )
    AS
                         SELECT @EmailCount = Count(*) FROM Users WHERE Email = @Email
     
                         RETURN @EmailCount
     
    Above stored procedures are written to check the availability of the User Name and Email.
    They have an input parameter for user name or email and an output parameter as user name count and email count.
     
  7. Don’t forget to include following namespaces in code behind file

    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
     
    C#
     
    using System.Data;
    using System.Data.SqlClient;
     
  8. Write below code in code behind file

    VB.NET
     
    Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click
           CheckUserNameAvailability()
           CheckEmailAvailability()
    End Sub
     
    Private Sub CheckUserNameAvailability()
           Dim myConnection As String = "Data Source=Local;Initial Catalog=SampleDB1431;Integrated Security=True"
           Dim connection As New SqlConnection(myConnection)
           Dim command As New SqlCommand("UserNameAvailability", connection)
           command.CommandType = CommandType.StoredProcedure
           command.Parameters.Add("@UserName", SqlDbType.VarChar, 100)
           command.Parameters("@UserName").Value = txtBoxUserName.Text
           command.Parameters.Add("@UserNameCount", SqlDbType.Int)
           command.Parameters("@UserNameCount").Direction = ParameterDirection.Output
     
           Dim count As Integer
     
           Try
                  connection.Open()
                  command.ExecuteNonQuery()
                  count = Convert.ToInt32(command.Parameters("@UserNameCount").Value)
           Catch ex As Exception
                  Response.Write(ex.Message)
           Finally
                  connection.Close()
           End Try
     
           If count >= 1 Then
                  imgUserName.ImageUrl = "Images/Unavailable.png"
                  lblUserName.Text = "Unavailable"
           Else
                  imgUserName.ImageUrl = "Images/Available.png"
                  lblUserName.Text = "Available"
           End If
    End Sub
     
    Private Sub CheckEmailAvailability()
           Dim myConnection As String = "Data Source=Local;Initial Catalog=SampleDB1431;Integrated Security=True"
           Dim connection As New SqlConnection(myConnection)
           Dim command As New SqlCommand("EmailAvailability", connection)
           command.CommandType = CommandType.StoredProcedure
           command.Parameters.Add("@Email", SqlDbType.VarChar, 200)
           command.Parameters("@Email").Value = txtBoxUserName.Text
           command.Parameters.Add("@EmailCount", SqlDbType.Int)
           command.Parameters("@EmailCount").Direction = ParameterDirection.Output
     
           Dim count As Integer
     
           Try
                  connection.Open()
                  command.ExecuteNonQuery()
                  count = Convert.ToInt32(command.Parameters("@EmailCount").Value)
           Catch ex As Exception
                  Response.Write(ex.Message)
           Finally
                  connection.Close()
           End Try
     
           If count >= 1 Then
                  imgEmail.ImageUrl = "Images/Unavailable.png"
                  lblEmail.Text = "Unavailable"
           Else
                  imgEmail.ImageUrl = "Images/Available.png"
                  lblEmail.Text = "Available"
           End If
    End Sub
     
     
    C#
     
    protected void btnRegister_Click(object sender, EventArgs e)
    {
           CheckUserNameAvailability();
           CheckEmailAvailability();
    }
    private void CheckUserNameAvailability()
    {
           string myConnection = "Data Source=Local;Initial Catalog=SampleDB1431;Integrated Security=True";
           SqlConnection connection = new SqlConnection(myConnection);
           SqlCommand command = new SqlCommand("UserNameAvailability", connection);
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.Add("@UserName", SqlDbType.VarChar, 100);
           command.Parameters["@UserName"].Value = txtBoxUserName.Text;
           command.Parameters.Add("@UserNameCount", SqlDbType.Int);
           command.Parameters["@UserNameCount"].Direction = ParameterDirection.Output;
     
           int count = 0;
     
           try
           {
                  connection.Open();
                  command.ExecuteNonQuery();
                  count = Convert.ToInt32(command.Parameters["@UserNameCount"].Value);
           }
           catch (Exception ex)
           {
                  Response.Write(ex.Message);
           }
           finally
           {
                  connection.Close();
           }
     
           if (count >= 1)
           {
                  imgUserName.ImageUrl = "Images/Unavailable.png";
                  lblUserName.Text = "Unavailable";
           }
           else
           {
                  imgUserName.ImageUrl = "Images/Available.png";
                  lblUserName.Text = "Available";
           }
    }
     
    private void CheckEmailAvailability()
    {
           string myConnection = "Data Source=Local;Initial Catalog=SampleDB1431;Integrated Security=True";
           SqlConnection connection = new SqlConnection(myConnection);
           SqlCommand command = new SqlCommand("EmailAvailability", connection);
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.Add("@Email", SqlDbType.VarChar, 200);
           command.Parameters["@Email"].Value = txtBoxUserName.Text;
           command.Parameters.Add("@EmailCount", SqlDbType.Int);
           command.Parameters["@EmailCount"].Direction = ParameterDirection.Output;
     
           int count = 0;
     
           try
           {
                  connection.Open();
                  command.ExecuteNonQuery();
                  count = Convert.ToInt32(command.Parameters["@EmailCount"].Value);
           }
           catch (Exception ex)
           {
                  Response.Write(ex.Message);
           }
           finally
           {
                  connection.Close();
           }
     
           if (count >= 1)
           {
                  imgEmail.ImageUrl = "Images/Unavailable.png";
                  lblEmail.Text = "Unavailable";
           }
           else
           {
                  imgEmail.ImageUrl = "Images/Available.png";
                  lblEmail.Text = "Available";
           }
    }
     
    Here is the explanation of the code
     
    CheckUserNameAvailability() function is written to call the UserNameAvailability stored procedure and check the availability of user name.
    Connection is established to SQL Server database using SqlConnection class.
    Stored procedure is called and parameters are added using SqlCommand object.
    I have called the ExecuteNonQuery() method and get the integer value which is the count of the user name.
    If count is greater than or equal to one that means a user name exists so that user name is unavailable otherwise it is available.
    CheckEmailAvailability() function is written to call the EmailAvailability stored procedure and check the availability of Email.
    Same functionality is repeated in this function as in CheckUserNameAvailability() function.
     
  9. Debug and view web site in browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to bind DropDownList and CheckBoxList to Dictionary in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net