Consider an example of search by name,
In database create a table that consist of names.
In design page,
<div>
Name to search
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:ToolkitScriptManager ID=”ToolkitScriptManager1″ runat=”server”>
</asp:ToolkitScriptManager>
<asp:AutoCompleteExtender ID=”AutoCompleteExtender1″ TargetControlID=”TextBox1″ runat=”server”
UseContextKey=”True” ServiceMethod=”GetCountries” MinimumPrefixLength=”1″
CompletionSetCount=”1″ CompletionInterval=”50″>
</asp:AutoCompleteExtender>
</div>
In code behind,
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string prefixText)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings[“dbconnection”].ToString());
con.Open();
SqlCommand cmd = new SqlCommand(“select name from tblName where name like @Name+’%'”, con);
cmd.Parameters.AddWithValue(“@Name”, prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> CountryNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][0].ToString());
}
return CountryNames;
}