Tuesday, February 1, 2011

Checking Username Availability using AJAX

Checking username in user registration page is common thing in these days. Now we can check how to this using AJAX., ie Check username availability without postback. To achieve this you need to know about Ajax. Ajax is Asynchronous JavaScript and XML. AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page [more].

Here I use two Class files and one Generic handler page to simplify code.

1: AjaxHelper
2: DataBaseHelper
3: Handler.ashx

Do not get confused, Client side javascript communicating sever with the help of Handler file, ie we send request to handler file, In the Handler file we create one instance of AjaxHelper Class and communicate with database with the help of DataBaseHelper Class

First we can check the sample images

User in Database

Now we can check how to send request to Handler Page, ie we do ajax calls to server, see the script below

Above we requested the handler file, Now we are going to check what handler file do when a request come from the client side.
first handler file check the query string. after create an instance of AjaxHelper class, it contain one boolean function called CheckUserNameAvailable(stirng username). if the value of CheckUserNameAvailable is true, it send response as "Available" else it send "Not Available".
public void ProcessRequest(HttpContext context)
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;
        if (!string.IsNullOrEmpty(request.QueryString["uname"]))
            AjaxHelper ajaxHelper = new AjaxHelper();
            bool available = ajaxHelper.CheckUserNameAvailable(request.QueryString["uname"]);
            if (available)
Username Available
"); } else { response.Write("
Username Not Available
"); } } }

Next is what is happening AjaxHelper Class, here is do not use any store procedure, if you need you can use SP. I already mentioned that this class contain only one method, public bool CheckUserNameAvailable(string username)
in this class I select the users in usertable which username is equal to requested username.
public class AjaxHelper
    public AjaxHelper()
    public bool CheckUserNameAvailable(string username)
        DataBaseHelper DBHelper = new DataBaseHelper();
        string cmdString = string.Format("SELECT username FROM usertable  WHERE username ='{0}'", username);
        string user = Convert.ToString(DBHelper.ExecuteScalar(cmdString));
        if (string.IsNullOrEmpty(user))
            return true;
            return false;

Check the DataBaseHelper Class, in DataBaseHelper class we read connection string form web.config file. then create sql connection, set sql command object, open sql connection, execute sql command, close sql connection, dispose command and connection.

public class DataBaseHelper
    SqlConnection con;
    SqlCommand cmd;

    public DataBaseHelper()
    public object ExecuteScalar(string commandText)
        con = Provider.GetConnection();
        cmd = new SqlCommand(commandText, con);
            return cmd.ExecuteScalar();
        { throw; }

public class Provider
    public static SqlConnection GetConnection()
        return new SqlConnection(ConfigurationManager.AppSettings["sqlConn"]);

Connection string in Web.Config


Download source username availability in Ajax