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

Introduction:

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

Description:

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.

Prerequisites:

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.

Implementation:

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:

 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        function checkIDAvailability() {
            $.ajax({
                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;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter your ID here
     <asp:TextBox ID="txtID" runat="server" onblur="checkIDAvailability();"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

C#:

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";
            }
            else
            {
                //User_Name is available
                result = "ID is available, you can use it";
            }
            //Return the result
            return result;
        }

Demo

Advertisements

, , , , , ,

  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: