You select all rows from the tbl_pincode table, and not just those that correspond to the characters (s) that the user enters. Then you assign a value from the area column for each row to zipcode.Text .
Perhaps try:
protected void autocomplete(object sender, EventArgs e) { if (string.IsNullOrEmpty(zipcode.Text)) return; string query = @" SELECT area FROM tbl_pincode WHERE area LIKE @Value UNION SELECT codes FROM tbl_pincode WHERE codes LIKE @Value"; SqlConnection conn = null; SqlCommand com = null; SqlDataReader dr = null; try { conn = new SqlConnection("Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;"); com = new SqlCommand(query, conn); string value = string.Format("{0}%", zipcode.Text); com.Parameters.AddWithValue("@Value", value); conn.Open(); dr = com.ExecuteReader(); if (dr.Read()) { zipcode.Text = dr.GetValue(0).ToString(); } } finally { if (conn != null) conn.Dispose(); if (dr != null) dr.Dispose(); if (com != null) com.Dispose(); } }
SQL selects the region and codes that begin with the characters that the user types so far. what I think based on your comments on other answers. I also modify the while and if while , as I see why you want to assign and then reassign the values ββfor the zipcode.Text property (I could understand if you were adding to the List control).
source share