Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to get ajax autocomplete extender work through the code behind, and trying to match the pattern from a database. but it doesn't work. Kindly please tell me what mistake I'm making.. Thank you.. :)

C#
<ajaxToolkit:AutoCompleteExtender ServiceMethod="SearchCustomers"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="TextBox_Search"
    ID="AutoCompleteExtender1"  runat="server" FirstRowSelected = "false">
</ajaxToolkit:AutoCompleteExtender>



C#
<asp:TextBox ID="TextBox_Search" runat="server" Height="28px" Width="206px"></asp:TextBox>


C#
public partial class MasterPage_UserProfile : System.Web.UI.MasterPage
{
    [System.Web.Script.Services.ScriptMethod]
    [System.Web.Services.WebMethod]
    public static List<string> SearchCustomers(string prefixText, int count)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Ebazzar"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select pname from products where " +"pname like @SearchText + '%'";
                cmd.Parameters.AddWithValue("@SearchText", prefixText);
                cmd.Connection = conn;
                conn.Open();
                List<string> Products = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        Products.Add(sdr["pname"].ToString());
                    }
                }
                conn.Close();
                return Products;
            }
        }
    }
}
Posted

1 solution

C#
//return type should be a array 
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static string[] SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Ebazzar"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            //create parameter like below
            cmd.CommandText = "select pname from products where pname like @SearchText";
            //set the parameter values as below
            cmd.Parameters.AddWithValue("@SearchText", prefixText + "%");
            cmd.Connection = conn;
            conn.Open();
            List<string> Products = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    Products.Add(sdr["pname"].ToString());
                }
            }
            conn.Close();
            return Products.ToArray();
        }
    }
}
 
Share this answer
 
v2
Comments
J. Chatterjee 11-May-14 23:42pm    
Nope. Nothing's happening. Don't know what's d problem
DamithSL 11-May-14 23:46pm    
which page you having this service method? is it on same page or seperate asmx file? put break point on start of your service method and check whether your application hit on the break point or not. debug and see what is going on.
J. Chatterjee 12-May-14 1:24am    
it is in my .aspx.cs file

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