Here I will explain how to use Ajax AutoCompleteExtender without using webservice to display the words begin with prefix typed into the textbox using asp.net
Description:
In previous article I explained clearly how to implement AjaxAutoCompleteExtender with webservice. We can attach Ajax autocomplete exteneder to any textbox to implement this and after assign autocomplete extender to textbox and type more content than the specified minimum word length, a popup will show words or phrases starting with that value. So the user can choose exact word from the popup panel. Here we are implementing autoCompleteextender to fetch data from the database without using Webservice.
Column Name
|
Data Type
|
Allow Nulls
|
ID
|
Int(set identity property=true)
|
No
|
CountryName
|
Varchar(50)
|
Yes
|
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
|
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax AutoCompleteExtender without Webservice</title>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ScriptManager1" runat="server"/>
<div>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCountry"
MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetCountries" >
</ajax:AutoCompleteExtender>
</div>
</form>
</body>
</html>
|
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;
}
|
After that set your database connection in web.config like this
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial
Catalog=MySampleDB"/>
</connectionStrings >
|
Demo
![]() |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
78 comments :
Thanks. Your code is very helpful for me. It is working fine.
DO NOT FIRING SERVICE METHOD BOSS
thanku very much... it works...
thanks for your explanation...
you explained properties of autocompleteextender...
it is awesome
didnot firing process
hi it's working fine i think you did mistake in your application
Hi Suresh
Your code is super
thanks.
The most important thing for me: the code provided in this example actually works. Thank you!
Thank you very much for publish this code
your site is very useful for me thanks
Good Post.It Really helped me in understanding AutoCompleteExtender.
Thanks.
its not working. somethings is missing.
pls complete this........
it's working fine please check your code without working how can i place demo..
Hi ur site is provide me good knowledge..
but this Example is not working. somethings is missing.
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List GetCountries(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnection"].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 CountryNames = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][0].ToString());
}
return CountryNames;
}
this is my codebehind code.
design page is same as ur page.
suresh thank you.It helped me a lot.
Code in vb.net
Very nice post! Perfectly working. Thanks!
this site is very helpful
Thank you Suresh. Your code is working perfectly fine when i use it in an aspx page.
If i put the same code in an user control AutoCompleteExtender is not working.
Any idea why it is not working ?
Thanks!
this code return only contryname how to find contry code..
thanks, really it is working...
i cant't see populate data
@suresh: your presentation very simple,easy to understand.. It really helped me a lot...
thank u sir....
Hi, you are posting always great post and they are help alot but your this code is not working I was trying many times even download your code and do that but not working
thanks..i need one more help
this code return contryname how to find SELECTED contry code.(that is ID)
hi team, my breakpoint is not reaching service method it ends at public webservice() {}. Please help
Great code, thanks a lot. It is working with an Access database (.accdb), but I can't get it to work with a sql server compact database (.sdf). Can you please help, please.
Thanks for your code. Working fine. Thanks alot.
hi Suresh Dasari, ur code is working great, can u tell me how to select multiple text ex:- india, indonesia, china using AutoComplete Extender .. appreciate ur help..
Prasad MSR
your code is working like magic.
thank you.
How can i retrieve ID and name, name is visible and ID is hidden, deal with them like a drop down list (DataValueFeild & DataTextFeild)
sorry...ur code is not working
@Unknown.... Please check this link http://www.aspdotnet-suresh.com/2012/07/jquery-autocomplete-textbox-with.html
This code is working fine please check your code i hope you did mistake in your code like forgot add to WebMethod attribute or sql connection problems etc...
I used successfully.But List appear on textbox.I want to display list below textbox.
I can not understand the "dbconnection" from
ConfigurationManager.ConnectionStrings["dbconnection"].ToString()
by how i can get it
ConfigurationManager.ConnectionStrings["dbconnection"].ToString() is your database connection string. Please check above code i added the that one also...
GREAT....................!!!!
it's really gr8....thnx yaar...
hey...its working great......keep uploading such a new things.....
its super yarr... working excellent..
Good day, a question as I can do to put more of an AutoCompleteExtender in a single website.
thanks.
Thank you Darling
THANKS A LOT, It helps me a lot with clear explanation.
Narendran
HI I got same requirement but bit change in requirement.I need to retrieve not one column i need to auto fill multiple columns with good design..please provide solution
Please click on below link for clarification
http://4.bp.blogspot.com/-o3RNiuUvqSw/UDSZ3WDY8GI/AAAAAAAAAO4/cuujP1m9_u0/s1600/dgviewmulitcol.jpg
dude this example is very helpful to me thank you...
hi..this is javeed.........
I am having the requirement of more than one textbox. how to add intellisense for that......can u help me please..........
Hi i like this solution but i also have another problem actually i want to Autogenerate A specific Id number in which first two digits are year ,second are Month and last four are serial number like 12110001 12-year,11-month,0001 serial no. and when my month will change then serial id will again set to 0001 i.e 12120001 can you help me
ThanQ sir
thanks..
your post are very use full For ME.. Really Thanks
How to restrict the multiple data with same name displayed in autocpmplete textbox...I only want display distinct name of each company from my database..? Please help me ?
How to restrict the multiple data with same name displayed in autocpmplete textbox...I only want display distinct name of each company from my database..? Please help me ? Help me sir I really need this..
I tried this code with mysql database. It's not firing. Can anybody help to solve this problem.,
My code is.,
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List GetCountries(string prefixText)
{
// protected string con = WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;
MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
MySqlCommand cmd = new MySqlCommand("select name from district where name like @Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", prefixText);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List CountryNames = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][1].ToString());
}
return CountryNames;
}
}
Connection string in webconfig file.,
AND I TRIED ALL TYPE OF AUTO COMPLETE TEXTBOX OPTIONS WITH MYSQL, WHICH YOU HAVE GIVEN. EVERYTHING IS TELLING ERROR OR NOT FIRING.,
PLEASE HELP ME TO CLEAR THIS PROBLEM.,
Nice Article, Thanks...
Amazing summary bookmarked the site.
thanks.............
Your post says "without using webservice" yet you do use a webservice!!!
good one it really worked for me..........
Thanks boss..it helped me
To get it work when deployed into iis is ...
Try...this
go to start>run>inetmgr>
In the connections sidepane..select application pools
select the application pool that u hav assigned when deployed that project into iis(to check that go to sites>in connections pane...and right click on website that u have deployed and select manage website >advanced settings then at the top u wil able to see application pool)
then comeback and select that particular application pool in application pools in connections pane right click on that particular application pool and select advanced settings..find out process model in that and select identity and browse through it and goto built in account and select local system.......then click ok...and get out of it...
I think it will work..it worked for me.....
It's actually very difficult in this busy life to listen news on Television, therefore I simply use the web for that purpose, and obtain the newest news.
Review my weblog; move next
Awesome....Working Fine..Thanks a lot...
nice One Suresh
super useful. love this tutorial, bro.
(totally not spambot)
Hi Suresh,
You may be right, it works, But some how event is not firing, I kept break points in VS2010, but they never hit, But on the same page i am using ajax control toolkit casecdeDropdownlist, it works well,
hi sir,
if i'm using two tables in sql database. how to search all the columns in the two tables using auto complete extender control in ajax. i need query for searching all the columns in the two tables..and retrieve data with their prefix string.. matching in all columns data.please help me.. thanks in advance
That is great, thanks your explanation.
thnkz a lot....! it wrkd
i used the same code. my webmethod countries
return all countryname start with the given character. But, in Textbox didn't fire the servicemethod.
Pls help me where i did mistake.
nice article yaar..it helped a lot
how to use multiple autocompleteextender within same page for multiple textboxes. Please help....
Service method is not firing..