Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have been trying to make my autocomplete work for the past few days.

I managed to find an article regarding this (http://www.codeproject.com/KB/aspnet/Autocomplete.aspx) and I have followed all the steps shown but still it is not working.

I am trying to get the autocomplete working by getting the food items from the food list database table which I have created in mySQL.

Please tell me what is the problem.

Below are the codes.

Thank you.

C#
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
   SqlConnection cn = new SqlConnection();
   DataSet ds = new DataSet();
   DataTable dt = new DataTable();
   String strCn = "Data Source=MY-PC;Initial Catalog=SurveyDB;Integrated Security=True";
   cn.ConnectionString = strCn;
   SqlCommand cmd = new SqlCommand();
   cmd.Connection = cn;
   cmd.CommandType = CommandType.Text;

   cmd.CommandText = "SELECT * FROM FoodList WHERE Food like @Food";
   cmd.Parameters.AddWithValue("@Food", "%" + prefixText + "%");

   try
   {
      cn.Open();
      cmd.ExecuteNonQuery();
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      da.Fill(ds);
   }
   catch
   {
   }
   finally
   {
      cn.Close();
   }
   dt = ds.Tables[0];

   List<string> txtItems = new List<string>();
   String dbValues;

   foreach (DataRow row in dt.Rows)
   {
      dbValues = row["Food"].ToString();
      dbValues = dbValues.ToLower();
      txtItems.Add(dbValues);
   }
   return txtItems.ToArray();
}

Below is the designer page:
ASP
<ajaxToolkit:AutoCompleteExtender
   ID="autoComplete1"  runat="server"
   EnableCaching="true"
   BehaviorID="AutoCompleteEx"
   MinimumPrefixLength="2"
   TargetControlID="TextBox1"
   ServicePath="AutoComplete.asmx"
   ServiceMethod="GetCompletionList"
   CompletionInterval="1000"
   CompletionSetCount="20"
                                    CompletionListCssClass="autocomplete_completionListElement"
   CompletionListItemCssClass="autocomplete_listItem"
                                    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
   DelimiterCharacters=";, :"
   ShowOnlyCurrentWordInCompletionListItem="true">
   <animations>
      <onshow>
      <sequence>
         <%-- Make the completion list transparent and then show it --%>
         <opacityaction opacity="0" />
         <hideaction visible="true" />

<%--Cache the original size of the completion list the first time the animation is played and then set it to zero --%>
         <ScriptAction Script="// Cache the size and setup the initial size
         var behavior = $find('AutoCompleteEx');
         if (!behavior._height) {
            var target = behavior.get_completionList();
            behavior._height = target.offsetHeight - 2;
            target.style.height = '0px';
         }" />
         <%-- Expand from 0px to the appropriate size while fading in --%>
         <parallel duration=".4">
         <fadein />
         <length propertykey="height" startvalue="0">
            EndValueScript="$find('AutoCompleteEx')._height" />
         </length></parallel>
      </sequence>
      </onshow>
      <onhide>
         <%-- Collapse down to 0px and fade out --%>
         <parallel duration=".4">
            <fadeout />
            <length propertykey="height" startvaluescript="<br" mode="hold" />
            "$find('AutoCompleteEx')._height" EndValue="0" />
         </parallel>
       </onhide>
    </animations>
                                
    <asp:TextBox ID="TextBox1" runat="server" Width="275px">
Posted
Updated 26-Aug-14 5:58am
v3
Comments
ZurdoDev 26-Aug-14 11:51am    
Put a breakpoint and see what is happening.

1 solution

What is the issue. Did you apply the css. You have to do troubleshoot it step by step. First check if data is coming from the web service method. If yes then something is wrong in your mark up code.Proceed one step at a time. Also in the article you mentioned, source code download is there. Did you try running that ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900