---
title: "JQuery autocomplete using handler"  
description: "JQuery autocomplete using handler"  
author: "Anonymous User"  
published: 2014-10-30  
updated: 2014-10-30  
canonical: https://www.mindstick.com/forum/2470/jquery-autocomplete-using-handler  
category: "jquery"  
tags: ["javascript", "javascript events"]  
reading_time: 2 minutes  

---

# JQuery autocomplete using handler

I' m [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to create a [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) box with [autocomplete](https://www.mindstick.com/forum/156169/what-is-google-suggest-or-autocomplete) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) and using a [handler](https://www.mindstick.com/forum/33532/handler-pagehandlerfactory-integrated-has-a-bad-module-managedpipelinehandler-in-its-module-list). I got all words from [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) but can not show them.

It' s jquery part:

```
$(function () {   $("#search-box").autocomplete({      source: "KeywordHandler.ashx",      minLength: 1,      //select: function (event, ui) {      //alert(ui.item.id + " / " + ui.item.value);      //}    });});
```

It' s handler part:\

```
public class KeywordHandler : IHttpHandler        {             public void ProcessRequest(HttpContext context)            {                string prefixText = context.Request.QueryString["term"];                using (SqlConnection conn = new SqlConnection())                {                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;                     using (SqlCommand cmd = new SqlCommand())                    {                         cmd.CommandText = "select Keyword from [dbo].[Log] where " + "Keyword like @SearchText + '%'";                        cmd.Parameters.AddWithValue("@SearchText", prefixText);                        cmd.Connection = conn;                         StringBuilder sb = new StringBuilder();                        conn.Open();                        using (SqlDataReader sdr = cmd.ExecuteReader())                        {                            while (sdr.Read())                            {                                sb.Append(sdr["Keyword"])                                    .Append(Environment.NewLine);                            }                        }                        conn.Close();                        context.Response.Write(sb.ToString());                    }                }            }             public bool IsReusable            {                get                {                    return false;                }            }        }
```

At handler, I can get all the words which I need but can not show them as offered [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords) at search box.\

## Replies

### Reply by Anonymous User

I added only Keyword class and returned it.

And it works.

```
public class KeywordHandler : IHttpHandler        {            class Keyword            {                public string value { get; set; }            }             public void ProcessRequest(HttpContext context)            {                string prefixText = context.Request.QueryString["term"];                using (SqlConnection conn = new SqlConnection())                {                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;                     using (SqlCommand cmd = new SqlCommand())                    {                         cmd.CommandText = "select distinct Keyword from [dbo].[Log] where " + "Keyword like @SearchText + '%' and ResultCount != 0";                        cmd.Parameters.AddWithValue("@SearchText", prefixText);                        cmd.Connection = conn;                         List<Keyword> suggestedList = new List<Keyword>();                        conn.Open();                        using (SqlDataReader sdr = cmd.ExecuteReader())                        {                            while (sdr.Read())                            {                                suggestedList.Add(new Keyword { value = sdr["Keyword"].ToString() });                            }                        }                        conn.Close();                         suggestedList.OrderBy(x => x.value).ToList();                         context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(suggestedList.Take(10)));                    }                }            }             public bool IsReusable            {                get                {                    return false;                }            }        }
```


---

Original Source: https://www.mindstick.com/forum/2470/jquery-autocomplete-using-handler

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
