Hi pooya1072,
The speed depends on the query result, network speed, server speed and the processor.
Please refer below sample.
HTML
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript">
$(document).ready(function () {
document.getElementById("txtUserName").onkeyup = function () {
var userName = document.getElementById("txtUserName").value;
var divElement = document.getElementById('label')
if (userName.length >= 3) {
jQuery.ajax({
url: 'RegisterationService.asmx/UserNameExists',
type: 'POST',
data: "{ userName: '" + userName + "'}",
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.d) {
document.getElementById('label').innerHTML = userName + ' is in Used'
document.getElementById('label').style.color = 'red'
document.getElementById("txtUserName").style.borderColor = "red"
}
else {
document.getElementById('label').innerHTML = userName + ' is available'
document.getElementById('label').style.color = 'green'
}
},
error: function (err) {
alert(err);
}
});
}
else {
document.getElementById('label').innerHTML = ''
document.getElementById("txtUserName").style.borderColor = ""
}
}
});
</script>
</head>
<body>
<input id="txtUserName" style="width: 50%" type="text" />
<br />
<div style="height: 30px; width: 50%;">
<label id="label">
</label>
</div>
</body>
</html>
Namespaces
C#
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
VB.Net
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data.SqlClient
Imports System.Data
Code (asmx WebService)
C#
[WebMethod]
public bool UserNameExists(string userName)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name FROM Customers WHERE Name = @Name", con))
{
cmd.Parameters.AddWithValue("@Name", userName);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt.Rows.Count > 0 ? true : false;
}
}
}
}
VB.Net
<WebMethod()>
Public Function UserNameExists(ByVal userName As String) As Boolean
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Name FROM Customers WHERE Name = @Name", con)
cmd.Parameters.AddWithValue("@Name", userName)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return If(dt.Rows.Count > 0, True, False)
End Using
End Using
End Using
End Function
Screenshot
