0

I am trying to use one of the best example for checking the username availability from the below site http://www.highoncoding.com/Articles/439_Performing_Instant_UserName_Availability_Check_Using_JQuery_Ajax_API.aspx

And It's just verifying with some pre-initiated names but I wan't to try with my database can anyone suggest me how to proceed further.

Here is the code:

    <script language="javascript" type="text/javascript">

var userName = '';

$(document).ready(function() 
{
    $("#txtUserName").blur(function()
    {
        userName = $(this).val(); 

        if(userName.length <= 6) 
        {
            $("#display").text("username must be atleast 7 characters"); 
            $("#display").css("background-color","red");
        }

        else 
        {
            $.ajax(
            {
                type:"POST",
                url:"AjaxService.asmx/CheckUserNameAvailability",
                data:"{\"userName\":\"" + userName + "\"}",
                dataType:"json",
                contentType:"application/json",
                success: function(response) 
                {
                    if(response.d == true) 
                    {
                        $("#display").text("username is available");
                         $("#display").css("background-color","lightgreen");
                    }
                    else
                    {
                      $("#display").text("username is already taken");
                         $("#display").css("background-color","red");
                    }
                }
            });
        }


    });
});


</script>

This AjaxService.asmx:

Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Linq
Imports System.Collections.Generic

Namespace JQueryUserNameAvailability


    <WebService([Namespace] := "http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo := WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    <System.Web.Script.Services.ScriptService> _
    Public Class AjaxService
        Inherits System.Web.Services.WebService
        <WebMethod> _
        Public Function CheckUserNameAvailability(userName As String) As Boolean
            Dim userNames As List(Of [String]) = New List(Of String)() From { _
                "azamsharp", _
                "johndoe", _
                "marykate", _
                "alexlowe", _
                "scottgu" _
            }

            Dim user = (From u In userNames Where u.ToLower().Equals(userName.ToLower())u).SingleOrDefault(Of [String])()

            Return If([String].IsNullOrEmpty(user), True, False)
        End Function

    End Class
End Namespace

Modified code:

 Public Function CheckUserNameAvailability(ByVal userName As String) As Boolean
    Dim strSql As String = String.Format("SELECT COUNT(UserNameCol) FROM Registration WHERE UserNameCol = '{0}'", userName)
    Dim strConnection As String = "Data Source=.\sqlexpress;Initial Catalog=Users;Integrated Security=True"
    Dim sqlConn As New SqlConnection(strConnection)
    Dim sqlDataAdap As New SqlDataAdapter(strSql, sqlConn)
    Dim dt As New DataTable()
    sqlDataAdap.Fill(dt)

    If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then
        Return False
    End If

    Return True
End Function

1 Answer 1

1

Modify your CheckUserNameAvailability function to get data from the table where you have saved all the username. An example:

Public Function CheckUserNameAvailability(userName As String) As Boolean
            Dim strSql as String = String.Format("SELECT COUNT(userNameCol) FROM users WHERE userNameCol = '{0}'", userName)
            Dim sqlConn as new SqlConnection(SQL Connection String)
            Dim sqlDataAdap as new SqlDataAdapter(strSql, sqlConn)
            Dim dt as new DataTable()
            sqlDataAdap.Fill(dt)

            If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then
               Return false
            End If

            Return true
        End Function
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply I have modified as shown above but it's not working.
what is theerror ur receiving? Did you set the connection string correctly?
I am not getting any error and here is my conn string <connectionStrings> <remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="Data Source=.\sqlexpress;Initial Catalog=Users;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
in case you are not getting any error, then check the names of table and column and if they are correct in debug check if it is actually filling the table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.