Check UserName Availability from database Using JQuery in asp.net
In this article we are going to validate the asp.net textbox control value (User name) with the database entry, if user name already exist in the database it will display the message like twitter style using JQuery and page method in asp.net. Sample Screen shown below

In this article we will see how to check the user name availability on Text change using JQuery in asp.net.
All you need add the reference the file in .aspx page like this
Lets first create a asp.net web page having one textbox control and a HTML span tag to display message
User Name :
Now we are also going to decorate the message displayed to the user on available or not available using css
So here is the css
Now we are going to write the JQuery Function to validate the User Name this JQuery function will also validate user name input should be more than 5 characters
Now Lets create a SQL table and name it to User_Table and to create it you can use Database wizard or you can use this SQL Script
CREATE TABLE [dbo].[User_Table](
[User_Name] [varchar](30) NOT NULL,
[First_Name] [varchar](50) NOT NULL,
[Last_Name] [varchar](50) NOT NULL,
[Middle_Name] [varchar](50) ,
[Email_Id] [varchar](70) NOT NULL,
CONSTRAINT [PK_User_Table] PRIMARY KEY CLUSTERED
(
[User_Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Now we have to create one stored procedure to validate the user Name exist in the database or not
CREATE PROCEDURE CheckUserName( @User_Name VARCHAR(50) )
AS
BEGIN
IF EXISTS (SELECT * FROM User_Table WHERE [User_Name] = @User_Name )
SELECT '1'; --user name already exist in database
ELSE
SELECT '0'; --user name does not exist in database
END
Now in your .cs file you need to create a PageMethod which will call the stored procedure and depend upon the user name availability it will return the string message to the JQuery function for displaying message to the user
So there is the Page method
If you see this method is Webservice method which will return the result and this function you can write in your Register.cs page.
You have to add this [System.Web.Services.WebMethod] attribute to the function which will check the username from the database.
In C# Example
[System.Web.Services.WebMethod]
public static string CheckUserName(string args)
{
string returnValue = string.Empty;
string conString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(conString);
try
{
SqlCommand sqlCmd = new SqlCommand("CheckUserName", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@User_Name", args.Trim());
sqlConn.Open();
int success = int.Parse((sqlCmd.ExecuteScalar().ToString()));
if(success == 1) // User Name Not Available
{
returnValue = "
'" + args + "' is already in use.";
}
else if(success == 0)//User_Name is available
{
returnValue = "Available";
}
}
catch
{
//Handle Error
}
finally
{
sqlConn.Close();
}
return returnValue;
}
And in VB.NET Sample
_
Public Shared Function CheckUserName(ByVal args As String) As String
Dim returnValue As String = String.Empty
Dim conString As String = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
Dim sqlConn As New SqlConnection(conString)
Try
Dim sqlCmd As New SqlCommand("CheckUserName", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@User_Name", args.Trim())
sqlConn.Open()
Dim success As Integer = Integer.Parse((sqlCmd.ExecuteScalar().ToString()))
If success = 1 Then
' User Name Not Available
returnValue = "
'" & args & "' is already in use."
ElseIf success = 0 Then
'User_Name is available
returnValue = "Available"
End If
Catch
'Handle Error
Finally
sqlConn.Close()
End Try
Return returnValue
End Function
And Now you are done..
Once you run the application you will see an example like this
If the username is available you will see this screen

If user Name is not available you will see this screen

If user has entered less than 5 character you will this see this screen

You can use the above JQuery function to validate any input with Database entry like Email and other stuff. Also if you want to validate on Button click than just change the control id and its event on what you want to validate.
Cheers!!!
Pankaj