Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to bind autocomplate ajax city table in sql 2008.plz help me for this code.
Posted
Comments
[no name] 16-Nov-12 1:56am    
What have you tried so far.??

1 solution

]]>
<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">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900