New solution using javascript not working with auto complete extender not working

Matt Matt 130 Reputation points
2026-01-10T00:47:44.5833333+00:00

This proposed solution is not working

<asp:TextBox ID="CityFetch" placeholder="Start typing the City name" type="text" runat="server" class="form-control" onkeyup="GetCities(this.value)"></asp:TextBox>

<asp:AutoCompleteExtender ID="AutoCompleteExtenderCity" ServiceMethod="GetCities" TargetControlID="CityFetch" EnableCaching="false" CompletionInterval="1000" MinimumPrefixLength="1" CompletionSetCount="1" runat="server"></asp:AutoCompleteExtender>

<script language="javascript" type="text/jscript">

function GetCities(prefixText, countryCode) {

   var countryCode = document.getElementById('<%= EUCountriesDD1.ClientID %>').value;

   PageMethods.GetCities(prefixText, countryCode, onSuccess, onError);

}

function onSuccess(cities) {

   // Handle the success response

}

function onError(error) {

   // Handle the error response

}

</script>

[System.Web.Script.Services.ScriptMethod()]

[System.Web.Services.WebMethod()]

public static List<string> GetCities(string prefixText, string countryCode)

{

 string strcon = ConfigurationManager.ConnectionStrings["xxxxx=l"].ConnectionString;

    //create new sqlconnection and connection to database by using connection string from web.config file

    SqlConnection con = new SqlConnection(strcon);

SqlCommand cmd = new SqlCommand("SELECT city FROM EUCities WHERE code = @CountryCode AND city LIKE @CityName + '%'", con);

con.Open();

cmd.Parameters.AddWithValue("@CountryCode", countryCode);

cmd.Parameters.AddWithValue("@CityName", prefixText);

        SqlDataAdapter sda = new SqlDataAdapter(cmd);  

        DataTable dt = new DataTable();  

        sda.Fill(dt);  

        List<string> cities = new List<string>();

   for (int i = 0; i < dt.Rows.Count; i++)

   {

       cities.Add(dt.Rows[i][0].ToString());

   }

   return cities;

}

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Matt Matt 130 Reputation points
    2026-01-10T02:55:48.94+00:00
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.