Tuesday 11 March 2014

AutoComplete Like Google Map Search

   

.Aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

 <asp:TextBox ID="txtCity" runat="server" ontextchanged="txtCity_TextChanged" Width="250px"></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCity"
         MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1"
         ServiceMethod="GetCity"></asp:AutoCompleteExtender>


.cs
      [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetCity(string prefixText)
        {
     
            DataTable dtAddress = new DataTable();
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmdAddress = new SqlCommand("SELECT Address+', '+City+', '+PostalCode+', '+Country AS Address FROM Sample where Address like '%'+@City+'%' or City like '%'+@City+'%' or PostalCode like '%'+@City+'%' or Country  like '%'+@City+'%'", con);
            cmdAddress.Parameters.AddWithValue("@City", prefixText);
            SqlDataAdapter adp = new SqlDataAdapter(cmdAddress);
            adp.Fill(dtAddress);
            List<string> CityNames = new List<string>();
            for (int i = 0; i < dtAddress.Rows.Count; i++)
            {
                CityNames.Add(dtAddress.Rows[i][0].ToString());
            }
           
            return CityNames;
            con.Close();
        }

No comments:

Post a Comment