N K N K - 5 months ago 40
Ajax Question

Asp.net ajax autocomplete not working with AutoCompleteExtender

I want to implement

ajax autocomplete
functionality in my textbox,

I tried like below

<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCountry"
ServicePath="~/App_Code/WebService.cs" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1"
CompletionInterval="1000" ServiceMethod="GetCompletionList">
</ajax:AutoCompleteExtender>


and in
Webservice.cs


I tried like below

[WebMethod]
public List<string> GetCompletionList(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name " +
"like '" + prefixText + "%' order by company_name", con);
//cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable( );
da.Fill(dt);
List<string> Company_name = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
Company_name.Add(dt.Rows[i][1].ToString());
}
return Company_name;
}
}


but when I am checking it, it is not working.

Also when I checked in the console it was giving something like


ScriptResource.axd?d=smCBrjG8b3peouydxeU-NDM-jIwTmeeFry083DH5OhjfnWTee0cYnYTLGZX9W7p4AmkPzE7GkZ-MoV…:6076 POST http://localhost:55264/PSITERP%20lite/App_Code/WebService.cs/GetCompletionList 403 (Forbidden)


pls suggest what is wrong here

Answer

Like this:

[WebMethod]
    public string[] GetCompletionList(string prefixText)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name  " +
                                                            "like '" + prefixText + "%' order by company_name", con);
            //cmd.Parameters.AddWithValue("@Name", prefixText);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable  dt = new DataTable( );
            da.Fill(dt);
            List<string> Company_name = new List<string>();
            for(int i=0;i<dt.Rows.Count;i++)
            {
                Company_name.Add(dt.Rows[i][1].ToString());
            }
            return Company_name.ToArray();
        }
    }
Comments