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.jQuery AJAX: Check Record Exists or Not using C# VB.Net and WebMethod<p>Hi&nbsp;<a class="username" rel="RahurkarP">RahurkarP,</a></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> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <p><strong>Page1.aspx</strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;[id*=txtFname]&#39;).on(&#39;change&#39;, function () { var findName = $(this).val(); if (findName != &#34;&#34;) { window.location.href = &#34;Default.aspx?findName=&#34; + findName; } }); }); &lt;/script&gt; &lt;table style=&#34;width: 40%;&#34;&gt; &lt;tr&gt; &lt;td&gt; First Name : &lt;/td&gt; &lt;td&gt; &lt;input type=&#34;text&#34; id=&#34;txtFname&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(document).ready(function () { var name = window.location.search.split(&#39;=&#39;)[1]; if (name != &#39;&#39;) { $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/IsExist&#34;, contentType: &#34;application/json;charset=utf-8&#34;, data: &#39;{findName:&#34;&#39; + name + &#39;&#34;}&#39;, dataType: &#34;json&#34;, success: function (data) { if (data.d == &#34;NotExist&#34;) { $(&#39;[id*=GrdEmployee]&#39;).hide(); } else { $(&#39;[id*=GrdEmployee]&#39;).show(); } }, error: function (result) { alert(result.responsetext); } }); } }); &lt;/script&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GrdEmployee&#34; EmptyDataText=&#34;No record&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString[&#34;findName&#34;] != null) { findData(Request.QueryString[&#34;findName&#34;].ToString()); } } } private void findData(string findName) { try { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString); conn.Open(); string query = &#34;select FirstName,LastName,City,Country from Employees WHERE FirstName LIKE @findName + &#39;%&#39;&#34;; SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue(&#34;@findName&#34;, findName); SqlDataReader reader = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(reader); GrdEmployee.DataSource = dt; GrdEmployee.DataBind(); } catch (Exception ex) { } } [WebMethod] public static string IsExist(string findName) { string isRecordExist = &#34;&#34;; try { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString); conn.Open(); string query = &#34;select Count(*) from Employees WHERE FirstName LIKE @findName + &#39;%&#39;&#34;; SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue(&#34;@findName&#34;, findName); int i = Convert.ToInt32(cmd.ExecuteScalar()); isRecordExist = i &gt; 0 ? &#34;Exist&#34; : &#34;NotExist&#34;; } catch (Exception ex) { } return isRecordExist; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then If Request.QueryString(&#34;findName&#34;) IsNot Nothing Then findData(Request.QueryString(&#34;findName&#34;).ToString()) End If End If End Sub Private Sub findData(ByVal findName As String) Try Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(1).ConnectionString) conn.Open() Dim query As String = &#34;select FirstName,LastName,City,Country from Employees WHERE FirstName LIKE @findName + &#39;%&#39;&#34; Dim cmd As SqlCommand = New SqlCommand(query, conn) cmd.Parameters.AddWithValue(&#34;@findName&#34;, findName) Dim reader As SqlDataReader = cmd.ExecuteReader() Dim dt As DataTable = New DataTable() dt.Load(reader) GrdEmployee.DataSource = dt GrdEmployee.DataBind() Catch ex As Exception End Try End Sub &lt;WebMethod()&gt; Public Shared Function IsExist(ByVal findName As String) As String Dim isRecordExist As String = &#34;&#34; Try Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(1).ConnectionString) conn.Open() Dim query As String = &#34;select Count(*) from Employees WHERE FirstName LIKE @findName + &#39;%&#39;&#34; Dim cmd As SqlCommand = New SqlCommand(query, conn) cmd.Parameters.AddWithValue(&#34;@findName&#34;, findName) Dim i As Integer = Convert.ToInt32(cmd.ExecuteScalar()) isRecordExist = If(i &gt; 0, &#34;Exist&#34;, &#34;NotExist&#34;) Catch ex As Exception End Try Return isRecordExist End Function</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/22nQrtD.gif" alt="" width="314" height="294" /></span></strong></p>https://www.aspforums.net:443/Threads/110701/jQuery-AJAX-Check-Record-Exists-or-Not-using-C-VBNet-and-WebMethod/https://www.aspforums.net:443/Threads/110701/jQuery-AJAX-Check-Record-Exists-or-Not-using-C-VBNet-and-WebMethod/Mon, 31 Dec 2018 01:19:49 GMT