Check Username availability in Page Load and keyup based on TextBox value using AJAX PageMethods in ASP.Net

Last Reply on Aug 17, 2017 06:44 AM By dharmendr

Posted on Aug 10, 2017 09:56 PM

 

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.CheckUsername
     @Email VARCHAR(255)
AS
BEGIN
      SET NOCOUNT ON;
      IF NOT EXISTS
            (SELECT * FROM T_Create_Profile
             WHERE Email = @Email
            )
            SELECT 'true'
      ELSE
            SELECT 'false'
END
GO


<System.Web.Services.WebMethod()> _
    Public Shared Function CheckUserName(ByVal userName As String) As String
        Dim returnValue As String = String.Empty
        Try
            Dim consString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
            Dim conn As New SqlConnection(consString)
            Dim cmd As New SqlCommand("CheckUsername", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Username", userName.Trim())
            conn.Open()
            returnValue = cmd.ExecuteScalar().ToString()
            conn.Close()
        Catch
            returnValue = "error"
        End Try
        Return returnValue
    End Function

<script type="text/javascript">
        function UsernameAvailability() {
            PageMethods.CheckUserName(document.getElementById("<%=txtUsername.ClientID%>").value, Username_OnSuccess);
        }
        function Username_OnSuccess(response) {
            var mesg = document.getElementById("Username_Check");
            
            switch (response) {
                case "true":
                    mesg.style.color = "";
                    mesg.innerHTML = "";
                    break;
                case "false":
                    mesg.style.color = "red";
                    mesg.innerHTML = "Username already taken.";
                    break;
            }
        }
        function Username_OnChange(txt) {
            document.getElementById("Username_Check").innerHTML = "";
        }
    </script>

                            
<span id="Username_Check"></span>

                            
<asp:TextBox ID="txtUsername" runat="server" onkeyup="Username_OnChange(this);UsernameAvailability();" ondrop="return false;" onpaste="return false;"></asp:TextBox>

I created a table called T_Create_Profile and in the table I added a column called Username. Then I added a data under Username called Will. Now After I run the program and typed “Will” into the textbox I was hoping to see the message saying “Username already taken” but I don’t see any message. Am I doing anything wrong?

 

By the way I tried to follow one of your tutorials titled “Check Username Availability in ASP.Net using AJAX PageMethods” but no luck. Please help. 

Results 1 - 5 of 11 123
Posted on Aug 10, 2017 09:56 PM
Hi @power,
Please try the following

Check whether username exists in database in ASP.Net

It might help you.

Cheers Andrea.

Posted on Aug 10, 2017 11:31 PM

Hi Andrea, I already tried the link you provided me but didn't help. Please take a good look at my question. 


Posted on Aug 11, 2017 03:15 AM Modified on on Aug 11, 2017 03:48 AM

Hi power,

I have created sample code which full-fill your requirement. So please refer the below code and modify as per your requirement.

HTML

<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <asp:TextBox ID="txtUsername" onChange="Username_OnChange(this)" onkeyup="keyupfunction(this)"
        runat="server" />
    <span id="Username_Check"></span>
</div>
<div>
    <script type="text/javascript">
        function UsernameAvailability(name) {
            PageMethods.CheckUserName(name, Username_OnSuccess);
        }

        function Username_OnSuccess(response) {
            var mesg = document.getElementById("Username_Check");

            switch (response) {
                case "true":
                    mesg.style.color = "";
                    mesg.innerHTML = "";
                    break;
                case "false":
                    mesg.style.color = "red";
                    mesg.innerHTML = "Username already taken.";
                    break;
            }
        }

        function Username_OnChange(txt) {
            document.getElementById("Username_Check").innerHTML = "";
            var mesg = document.getElementById("Username_Check");
            mesg.innerHTML = "";
        }

        function keyupfunction(d) {
            UsernameAvailability(d.value)
        }
    </script>
</div>
</form>

C#

[System.Web.Services.WebMethod()]
public static string CheckUserName(string userName)
{
    string returnValue = string.Empty;
    try
    {
        string consString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection conn = new SqlConnection(consString);
        SqlCommand cmd = new SqlCommand("CheckUsername", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Username", userName.Trim());
        conn.Open();
        returnValue = cmd.ExecuteScalar().ToString();
        conn.Close();
    }
    catch
    {
        returnValue = "error";
    }
    return returnValue;
}

Vb.Net

    <System.Web.Services.WebMethod()> _
    Public Shared Function CheckUserName(ByVal userName As String) As String
        Dim returnValue As String = String.Empty
        Try
            Dim consString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
            Dim conn As New SqlConnection(consString)
            Dim cmd As New SqlCommand("CheckUsername", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Username", userName.Trim())
            conn.Open()
            returnValue = cmd.ExecuteScalar().ToString()
            conn.Close()
        Catch
            returnValue = "error"
        End Try
        Return returnValue
    End Function

 

 

 


Posted on Aug 11, 2017 10:45 AM

Hello Indresh,

Thank you for your sample. I adjusted few things to meet my requirement, however, it didn't help me at all. Did you try your sample at all and did it work for you? My original question was based on one of the sample examples written by Mudassar. If possible could you ask him to look at my original question since my question is identical to his sample?

Thank you. 


Posted on Aug 14, 2017 04:22 AM

Hi power,

Check the parameter you have passed.

In procedure you are passing @Email as parameter and in code behind you have used @Username as parameter. Thats the wrong you are doing.