JQUERY AUTOCOMPLETE USING HANDLER

Andrew Watson

Total Post:39

Points:273
Posted by  Andrew Watson
 1105  View(s)
Ratings:
Rate this:

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.

  1. Brad Pitt

    Post:61

    Points:429
    Re: JQuery autocomplete using handler

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

      Modified On Apr-06-2018 01:51:48 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!