]]>
<html>
<head runat="server">
<title>Ajax AutoCompleteExtender without Webservice</title>
</head>
<body>
<form id="form1" runat="server">
<ajax:toolkitscriptmanager id="ScriptMgr1" runat="server" xmlns:ajax="#unknown">
<asp:textbox id="txtCountry" runat="server" xmlns:asp="#unknown">
<ajax:autocompleteextender id="AutoCompleteExtender1" runat="server" targetcontrolid="txtCountry" xmlns:ajax="#unknown">
MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetCountries" >
</form>
</body>
</html>
//// in c#
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
[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 * from Country where CountryName 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][1].ToString());
}
return CountryNames;
}
note use same parameter name string prefixText in List<string> GetCountries(string prefixText) method.
After that set your database connection in web.config like this
<connectionstrings>
<add name="dbconn" connectionstring="Data Source=subodhchauhan;Integrated Security=true;Initial Catalog=SampleDb">