How to check the ID availability in Database using Jquery Ajax.


In this article I am going to explain how to check the data availability in database using Jquery Ajax.


Here I am going to explain one of the options to check if record is available in database and show error message according to the availability. This method or logic which I have explained here can be useful to check if the username is already used or it is available to use.


You need to add the necessary JQuery files to your page prior to use this plugin. In this example I have used the online JQuery CDN files available. If you don’t want to use the online available cdn file then you can manually add the jquery files to your solution. I have explained the steps here in detail.


Here we are make use of Jquery Ajax post method in Jquery to check if record exists in database or not on the blur event of textbox.


<html xmlns="">
<head runat="server">
    <script src=""></script>
    <script type="text/javascript">
        function checkIDAvailability() {
                type: "POST",
                url: "JqueryAjaxPost.aspx/checkUserName",
                data: "{IDVal: '" + $("#<% =txtID.ClientID %>").val() + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: onSuccess,
                failure: function (AjaxResponse) {
                    document.getElementById("Label1").innerHTML = "Error Occured";
            function onSuccess(AjaxResponse) {
                document.getElementById("Label1").innerHTML = AjaxResponse.d;
    <form id="form1" runat="server">
        Enter your ID here
     <asp:TextBox ID="txtID" runat="server" onblur="checkIDAvailability();"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>


This method will get called from clientside using Jquery Ajax. You need to ensure that you have added the WebMethod attribute to the method.

        //Server Side Method which will get called from Client Side
        //Ensure that you have declared the method like WebMethod
        [System.Web.Services.WebMethod(EnableSession = true)]
        public static string checkUserName(string IDVal)
            string result = string.Empty;
            //Get your connection string here
            string conString = System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2008R2ConnectionString2"].ConnectionString;
            //Change your query here
            string qry = "Select AddressID from Person.Address Where AddressID =@AddressID";
            SqlDataAdapter da = new SqlDataAdapter(qry, conString);
            //Pass the value to paramter
            da.SelectCommand.Parameters.AddWithValue("@AddressID", IDVal.Trim());
            DataSet ds = new DataSet();
            da.Fill(ds, "IDTable");
            //Check if dataset is having any value
            if (ds.Tables["IDTable"].Rows.Count > 0)
                // User Name Not Available
                result = "ID already in use";
                //User_Name is available
                result = "ID is available, you can use it";
            //Return the result
            return result;



, , , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: