Adsence750x90

Tuesday, February 1, 2011

Checking Username Availability using ASP.net 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)
            {
                response.Write("
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;
        else
            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);
        con.Open();
        try
        {
            return cmd.ExecuteScalar();
        }
        catch
        { throw; }
        finally
        {
            cmd.Dispose();
            con.Close();
            con.Dispose();
        }
    }
}




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


Connection string in Web.Config

    
  

Download source username availability in asp.net Ajax

10 comments:

Anil Kumar Pandey said...

Nice Piece of code, I like this article..

Raju.M said...

Anil... thanks for your comment.

aayyaa said...

Really nice code tutorial!

But how to check user availability if I am using vb.net in windows based application?
Could you help me give some ideas?
thanks in advanced

Praveen kumar said...

Excellent Code... link to download is not working...

Raju.M said...

@Praveen..
Thanks for the Info. I will update soon.

felix said...

This very Use ful for me . Download link not working.

meghna venkat said...

I tried this code its working well but when i use master page it doesn't work. Please give me a solution ASAP.

Raju.M said...

@meghna venkat on what id you are binding the response fron hander page. Normally if we add master page our control id will change to 'ct100..' so please check your controls id using firebug for firefox or IEWebDeveloper tools for IE

Thanks

Gaurav Balyan said...

Thanks very nice tutorial

Gaurav Balyan said...

Really Very Nice Tutorial