Search code examples
c#asp.netautocompleteextender

ASP.NET AJax Toolkit AutoCompleteExtender Issue


Trying to implement the AutoCompleteExtender and coming up with a deadend Internal Server Error 500.

Page Method:

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> getcompletionlist(string prefix, int count)
    {
        string[] dccfields = null;
        List<string> ret = new List<string>();

        try
        {
            dccfields = DCC.get_field_names();
            return OFControls.get_autocomplete_list(dccfields, prefix);
        }
        catch (Exception ex)
        {
            ret.Add("!" + ex.Message);
            return ret;
        }
    }

aspx page:

                                    <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
                                    <asp:AutoCompleteExtender 
                                        ServiceMethod="getcompletionlist" 
                                        MinimumPrefixLength="1"  
                                        CompletionInterval="10" 
                                        EnableCaching="false" 
                                        CompletionSetCount="1" 
                                        TargetControlID="TextBox12"  
                                        ID="AutoCompleteExtender1" 
                                        runat="server" 
                                        FirstRowSelected="false" 
                                        UseContextKey="True">  
                                    </asp:AutoCompleteExtender>

Coming up with error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) [http://localhost:52966/QBIntegration.aspx]

I've tried adding Service Path - No change. Also specified another method name and got the 404 not found error so it seems that the code is being found just will not run. I also know that it is trying to load it because the 500 error comes up only when typing code in the textbox.

Also ... I do have the toolkitsriptmanager in the master page. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true"></asp:ToolkitScriptManager>

Thanks in advance


Solution

  • The last few (if not more) of the AjaxToolKit does NOT require the AjaxScriptManager - it has been deprecated. You are now to use the "standard" ScriptManager (hooray for all!!!!).

    So, a minimal working example will look like this:

    Markup: (this is not a master/child page setup).

    <body>
        <form id="form1" runat="server">
    
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <div>
                <h3>Select A Hotel</h3>
                <asp:TextBox ID="TextBox1" runat="server" Width="254px"></asp:TextBox>
    
                <ajaxToolkit:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender"
                    runat="server" BehaviorID="TextBox1_AutoCompleteExtender" 
                    DelimiterCharacters=""  TargetControlID="TextBox1"
                    CompletionInterval="100"
                    MinimumPrefixLength="1"
                    ServiceMethod="SearchCustomers" >
                </ajaxToolkit:AutoCompleteExtender>
            </div>
    
        </form>
    </body>
    

    So, note how we use the standard script manager. (you should not even see the older script manager in the tool box anyway - as noted, it is depreciated (it caused too many issues - and having two script managers on the same page amounts to a really big hairy ball of js code anyway).

    So, so with above, then we need our routine.

    This works for me:

        [WebMethod()]
        public static List<string> SearchCustomers(string prefixText, int count)
        {
            List<string> customers = new List<string>();
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                string strSQL = "SELECT HotelName FROM tblHotels " +
                                "WHERE HotelName like @SearchText + '%' ORDER BY HotelName";
    
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    cmdSQL.Parameters.Add("@SearchText", SqlDbType.NVarChar).Value = prefixText;
                    conn.Open();
                    SqlDataReader sReader = cmdSQL.ExecuteReader();
                    while (sReader.Read())
                        customers.Add(sReader["HotelName"].ToString());
                }
            }
            return customers;
        }
    

    And the result is this - I typed in K and a drop pop does show:

    enter image description here

    So, if you have the older ajax script manager? I would suggest and attempt to update (upgrade) to a newer version of ajaxtoolkit - since you now only need one script manager for pages.

    You can use nuget for this. (but, I can't recall the details, but upgrading a existing site had some issues. But, it was WELL WORTH the efforts to get this down to use one common and standard ScriptManager - and not two of them.

    The only perhaps "issue" in your code? does this return a list of string??

    dccfields = DCC.get_field_names();
    OFControls.get_autocomplete_list(dccfields, prefix);