Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
WebService for Checking UserName
Posted
Comments
BobJanova 10-Aug-11 8:35am    
Unclear. What do you actually want to do?

google WebService for Checking UserName
 
Share this answer
 
Here is the solution for checking UserName availability using web service with AJAX enabled page method...

place the following script in head tag....

Script :

C#
<script type="text/javascript">
        var usernameCheckerTimer;
        function usernameChecker(username) {
            var spanAvailability = $get("spanAvailability");
            clearTimeout(usernameCheckerTimer);
            if (username == "") {
                spanAvailability.innerHTML = "Code cannot be Blank";
            }
            else {
                spanAvailability.innerHTML = "checking...";
                usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 750);
            }
        }

        function checkUsernameUsage(username) {
            PageMethods.IsUserAvailable(username, OnSucceeded);
        }
        function OnSucceeded(result, userContext, methodName) {
            var spanAvailability = $get("spanAvailability");
            if (methodName == "IsUserAvailable") {
                if (result == true)
                    spanAvailability.innerHTML = "Already Exists";
                else
                    spanAvailability.innerHTML = "Does Not Exist";
            }
        }
    </script>



Place the following design code with in the form tag...

Design :

XML
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
   </asp:ScriptManager>
   <div>
       <table>
           <tr>
               <td>
                   User Name
               </td>
               <td>
                   <asp:TextBox ID="TextBox1" runat="server" onblur="usernameChecker(this.value);"></asp:TextBox>
                   <span id="spanAvailability"></span>
               </td>
           </tr>
       </table>
   </div>



Code Behind :


C#
protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static bool IsUserAvailable(string username)
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=maj-056\sqlexpress;Initial Catalog=Sample;Integrated Security=True;Pooling=False"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tblLogin where UserName='" + username + "'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                return true;
            }
            else
            {
                return false;
            }
            con.Close();
        }
    }
 
Share this answer
 
v2
Comments
BobJanova 10-Aug-11 8:34am    
Good answer, but not actually a web service ... quite possibly what the questioner actually wants though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900