views:

1941

answers:

4

I've tried to the letter to search for mistakes in my code, but i can't myself get that autocomplete extender to work. Help wanted.

Here's my code: (excerpt from my aspx page)

  <asp:TextBox ID="TextBox1" Width="120px" runat="server"></asp:TextBox>
    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ServiceMethod="GetCompletionList" ServicePath="SearchAutoComplete.asmx" MinimumPrefixLength="1">
    </cc1:AutoCompleteExtender>

My Webservice code:

 [WebMethod]
    public static string[] GetCompletionList(string prefixText, int count)
    {
        List<string> returnData = new List<string>();
        MySqlConnection con = new MySqlConnection(Connection.ConnectionString());
        string sql = "select title from blog where title like '%" + prefixText + "%'";
        MySqlCommand cmd = new MySqlCommand(sql, con);
        con.Open();
        MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader.Read())
        {
            returnData.Add(reader["title"].ToString());
        }
        return returnData.ToArray();
    }
+1  A: 

How to troubleshoot this:

Comment out your SQL code. Just return an array with some test data. Does that work? Do you see it? If not, your webservice code is not getting called. If that works, your problem is with your database code.... Is your webservice code on the calling page?

Dave Markle
A: 

I think your problem is that the GetCompletionList method is declared static.

If you run up just the .asmx code in a debugger session (or browse to the .asmx file if you have deployed your code to a webserver) you should see a list of available operations for the web-service. When I change the code in the Ajax control toolkit examples to declare this method as static the operation is no longer in the list and the autocomplete extender also stops working.

Change your method signature to:

public string[] GetCompletionList(string prefixText, int count)
David Hall
+2  A: 

As well as the GetCompletionList method being incorrectly declared as static, it needs to have two attributes; [System.Web.Services.WebMethod] and [System.Web.Script.Services.ScriptMethod]

So your declaration should look like this:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count) { ...

Also your service class should have the following attributes:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]

The autocomplete extender will also appear to be broken if your GetCompletionList method throws an exception. To guard against this you should add a try..catch block around the code the function

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{
    List<string> returnData = new List<string>();

    try
    {
        // database access code that sets returnData
    }
    catch (Exception ex)
    {
        // log the exception
    }

    return returnData.ToArray();
}
Richard Ev
A: 

Add reference to your web service in ScriptManager like this

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
 <asp:ServiceReference Path="AutoComplete.asmx" />
 </Services>
 </asp:ScriptManager>

refer link below for more info

Ajax AutoComplete textbox in gridview

amiT jaiN