Search code examples
asp.netwebmethod

Why is this code not working?


I am trying to add a search bar with an auto complete in asp.net using a web method and here is what i tried :

 <asp:TextBox runat="server" ID="sBox"></asp:TextBox>

        <asp:AutoCompleteExtender runat="server" ID="aC" TargetControlID="sBox"
            MinimumPrefixLength="2" Enabled="true" EnableCaching="true"
                CompletionInterval="0000" CompletionSetCount="20"
                    ServiceMethod="AutoComplete" ServicePath="~/Controls/SearchComplete.asmx"
                ></asp:AutoCompleteExtender>

and the web-method:

    [WebMethod]
public string[] AutoComplete(string prefixText) //auto completing the searchbar
{
    List<string> listString = new List<string>();

    using (SqlConnection conn = new SqlConnection(@"ConnectionString"))
    {
        SqlCommand cmd = new SqlCommand("SELECT id,name FROM search WHERE name LIKE @name", conn);
        cmd.Parameters.AddWithValue("@name", "%" + prefixText + "%");

        conn.Open();

        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows) 
        {
            while (dr.Read()) 
            {
                listString.Add(AutoCompleteExtender.CreateAutoCompleteItem(dr["name"].ToString(), dr["id"].ToString()));
            }
        }
    }

    string[] str = listString.ToArray();
    return str;
}

it's not giving errors it just isn't auto completing , thanks

using asp.net 4.0


Solution

  • Two things to check for:

    1) check to see if there's data going TO the server

    2) put a break point on the "return" statement to checked what's being returned from the query