Check this code
<pre><asp:TextBox ID="Name" runat="server" onchange="checkName()" />
<pre><div id="checkName" runat="server">
<asp:Label ID="lblStatus" runat="server"></asp:Label></div>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function checkName() {
$.ajax({
type: "POST",
url: "URL Here",
data: '{Name: "' + $("#<%=txtName.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response);
}
});
}
function OnSuccess(response) {
}
}
</script>
Code Behind
[System.Web.Services.WebMethod]
public static string CheckName(string Name)
{
string retval = "";
SqlConnection con = new SqlConnection("data source");
con.Open();
SqlCommand cmd = new SqlCommand("select Name from tableName where Name=@Name",con);
cmd.Parameters.AddWithValue("@Name", Name);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
retval = "true";
}
else
{
retval = "false";
}
return retval;
}