ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Check Username availability while typing using WebService in AngularJS<p>Hi&nbsp;rani,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <pre class="brush: sql">CREATE PROCEDURE [dbo].[CheckUserAvailability] @UserName VARCHAR(50) AS BEGIN SET NOCOUNT ON; IF NOT EXISTS (SELECT UserName FROM Users WHERE UserName = @UserName) BEGIN SELECT &#39;TRUE&#39; END ELSE BEGIN SELECT &#39;FALSE&#39; END END</pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var app = angular.module(&#39;MyApp&#39;, []) app.controller(&#39;MyController&#39;, function ($scope, $http) { $scope.Message = &#34;&#34;; $scope.CheckAvailability = function () { if ($scope.Username.length &gt; 0) { $http({ method: &#34;POST&#34;, url: &#34;WebService.asmx/CheckUsername&#34;, dataType: &#39;json&#39;, data: &#39;{username: &#34;&#39; + $scope.Username + &#39;&#34; }&#39;, headers: { &#34;Content-Type&#34;: &#34;application/json&#34; } }).success(function (data) { if (data.d) { $scope.Color = &#34;green&#34;; $scope.Message = &#34;Username available.&#34;; } else { $scope.Color = &#34;red&#34;; $scope.Message = &#34;Username not available.&#34;; } }); } else { $scope.Message = &#34;&#34;; } } }); &lt;/script&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; User Name : &lt;input type=&#34;text&#34; ng-model=&#34;Username&#34; ng-keyup=&#34;CheckAvailability()&#34; /&gt;&lt;br /&gt; &lt;span ng-bind=&#34;Message&#34; ng-style=&#34;{color:Color}&#34;&gt;&lt;/span&gt; &lt;/div&gt;</pre> <p><strong><span style="text-decoration: underline;">Webservice</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] public bool CheckUsername(string username) { bool status = false; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;CheckUserAvailability&#34;, conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(&#34;@UserName&#34;, username.Trim()); conn.Open(); status = Convert.ToBoolean(cmd.ExecuteScalar()); conn.Close(); } } return status; } } </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Web Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services &#39; To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. &lt;System.Web.Script.Services.ScriptService()&gt; _ &lt;WebService(Namespace:=&#34;http://tempuri.org/&#34;)&gt; _ &lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; _ &lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; _ Public Class WebService Inherits System.Web.Services.WebService &lt;WebMethod()&gt; Public Function CheckUsername(ByVal username As String) As Boolean Dim status As Boolean = False Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using conn As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;CheckUserAvailability&#34;, conn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.AddWithValue(&#34;@UserName&#34;, username.Trim()) conn.Open() status = Convert.ToBoolean(cmd.ExecuteScalar()) conn.Close() End Using End Using Return status End Function End Class</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/JwSHR6P.gif" alt="" width="259" height="62" /></span></strong></p>https://www.aspforums.net:443/Threads/111364/Check-Username-availability-while-typing-using-WebService-in-AngularJS/https://www.aspforums.net:443/Threads/111364/Check-Username-availability-while-typing-using-WebService-in-AngularJS/Thu, 11 Jul 2019 04:14:24 GMT