JQuery autocomplete using handler

I' m trying to create a search box with autocomplete property and using a handler. I got all words from database 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 at search box.

Last updated:10/30/2014 11:44:13 PM

1 Answers

Anonymous User
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;
                }
            }
        }

Answer