aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Autocomplete Clear Textbox if No Match Found in Asp.Net

May 27, 2015

Here I will explain how to use
jQuery to clear autocomplete textbox if no records match found in using c#, or jQuery autocomplete clear textbox if no records match found while searching in using c#,

In previous articles I explained jQuery show or hide loading image in, jQuery autocomplete textbox with multiple words in, jQuery autocomplete with images in,
jQuery slideup, slidedown, slidetoggle effects example and many articles relating to autocomplete, css, AngularJS, jQuery, JavaScript and Now I will explain how to clear in jQuery autocomplete textbox in with example.

Before implement this example first design one table userdetails in your database like as shown below

Column Name
Data Type
Allow Nulls
Once we design table in our database insert some dummy data in your table to use it in our application that would be like as shown below

Now open your aspx page and write the code like as shown below

<html xmlns="">
<title>jQuery Show Records Found Message in AutoComplete</title>
<link href="" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function () {
function SearchText() {
source: function (request, response) {
type: "POST",
contentType: "application/json; charset=utf-8",
url: "jQueryAutoCompleteTextbox.aspx/GetAutoCompleteData",
data: "{'username':'" + $('#txtSearch').val()  + "'}",
dataType: "json",
success: function (data) {
if (data.d.length >0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
else {
response([{ label: 'No Records Found', val: -1}]);
error: function (result) {
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
<form id="form1" runat="server">
<div class="demo">
<asp:HiddenField ID="hdnId" runat="server" />
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<input type="text" id="txtSearch" class="autosuggest" />
Selected UserId:<b><label id="lblUserId" /></b>

Now add following namespaces in code behind

C# Code

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data.SqlClient;

After completion of adding namespaces you need to write the code like as shown below

public static List<string> GetAutoCompleteData(string username)
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
using (SqlCommand cmd = new SqlCommand("select UserId,UserName from userdetails where UserName LIKE '%'+@SearchText+'%'", con))
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
result.Add(string.Format("{0}/{1}", dr["UserName"], dr["UserId"]));
return result;


Imports System.Collections.Generic
Imports System.Web.Services
Imports System.Data.SqlClient

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
<WebMethod()> _
Public Shared Function GetAutoCompleteData(ByVal username As String) As List(Of String)
Dim result As New List(Of String)()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("select UserId,UserName from userdetails where UserName LIKE '%'+@SearchText+'%'", con)
cmd.Parameters.AddWithValue("@SearchText", username)
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
result.Add(String.Format("{0}/{1}", dr("UserName"), dr("UserId")))
End While
Return result
End Using
End Using
End Function
End Class


jQuery Autocomplete Clear Textbox if No Match Found in Asp.Net

Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.