AspdotnetCodes.com
Voted 'Best ASP.NET Host' for 2008 – DiscountASP.NET
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 AutoComplete From Database
Posted by Moderator1 on 6/11/2007 10:54:58 AM Category: AJAX
Total Views : 22732
Add to my favorites
Email to friend
  
Introduction
This article explains to fill a AutoComplete Textbox from the data fetched from database.
Description
Everyone knows about the AutoComplete Textbox. It is an ASP.NET AJAX extender that can be attached to any TextBox control. When the user types some letters in the Textbox, a popup panel will come to action and displayed the related words. So that the user can choose exact word from the popup panel. Here I tried to explain how this AutoComplete fetches data from the database through a Webservice.
Open Microsoft Visual Studio, click on New Website. Then choose ASP.NET Ajax Enabled Website and change the location to point your http://localhost/AutoComplete folder. Obviously, Default.aspx is added to your solution explorer.

Now drag and drop a Textbox from your Toolbox. Then drag and drop a ScriptManager and AutoCompleteExtender to your Default.aspx page. Then add a webservice to your project as WebService.asmx. First thing you have to do is to add the ScriptService reference to the webserive as follows.
[System.Web.Script.Services.ScriptService]
Now, write a webmethod ‘GetCountryInfo’ to fetch the data from the country table as follows
[WebMethod]
public string[] GetCountryInfo(string prefixText)
{
 int count = 10;
 string sql = "Select * from Country Where Country_Name like @prefixText";
 SqlDataAdapter da = new SqlDataAdapter(sql,”Your Connection String Comes Here”));
 da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
 DataTable dt = new DataTable();
 da.Fill(dt);
 string[] items = new string[dt.Rows.Count];
 int i = 0;
 foreach (DataRow dr in dt.Rows)
 {
  items.SetValue(dr["Country_Name"].ToString(),i);
  i++;
 }
 return items;
}
The above webmethod takes prefixText as argument, sends it to the query to fetch only the related words that starts with the prefixText values. Then it returns the result as an array of strings.

Next, in the Default.aspx page, set the AutoCompleteExtender’s TargetControlID property to the TextBox Id. Now you can see a new Extenders Tab is added in the Textbox’s Property window. Set ServicePath as WebService.asmx, ServiceMethod as GetCountryInfo and MinimimPrefixLength as 1.
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1" ServiceMethod="GetCountryInfo" ServicePath="WebService.asmx" TargetControlID="TextBox1"> </cc1:AutoCompleteExtender>
Now, its time to run the project. Select the Default.aspx and click on View in Browser. You can see the excellent application starts to run. Type your country’s first letter. See all the countries starts with that letter will appear in the popup.

Else if you would like to see the application, it is just below.. 

Enter Your Country :
 
Viewer's Comments
Posted by Excellent on 7/1/2007 11:54:12 PM
Thanks a ton man, I couldn't find the correct parameter to get the textbox value to my webservice. Appreciate it. Tim
 
Posted by Bhasker Reddy on 7/17/2007 6:49:49 AM
This article was very good it helps me lot while deloping the auto complete functionality for text box
 
Posted by G on 7/24/2007 1:39:12 PM
Do you know if there are any issues with implementing the same logic for an oracle back end ? (I am trying to fill it a text box using oracle client library fetching results in a data set using stored procedures)
 
Posted by c on 7/27/2007 2:24:00 AM
This is a good article. it helps to use autocomplete extender for text box.
 
Posted by c on 7/27/2007 2:24:05 AM
This is a good article. it helps to use autocomplete extender for text box.
 
Posted by Abdulyours on 8/2/2007 4:02:23 AM
Good article...But it will be good if we get the same functionality without using webservice....Is there any sample for that ?
 
Posted by Uma Ramiya on 8/27/2007 4:15:12 AM
Ajax Control AutoCompleteExtender is missing in my .Net framework Components. How to go about it
 
Posted by Alan on 8/27/2007 4:14:42 PM
Uma: I had to download from the link below, and added the AjaxControlToolKit.DLL to my VS 2005 toolbox. http://www.codeplex.com/AtlasControlToolkit/Release/ProjectReleases.aspx?ReleaseId=1425 For some reason this is not included with the Microsoft Ajax release, not clear why.
 
Posted by Shankar on 9/27/2007 4:23:02 AM
This is a good article. it helps to use autocomplete extender for text box at the right time. Thanks Dear
 
Posted by Bhargavi on 10/10/2007 2:19:30 AM
I did all the steps given here. I had declared a static dataset and filled with country Name in Page load of Default.aspx. In the Webmethod i do a search in the Dataset DataTable that has been filled already using the "Select" method of a Datatable I add All the results to String Item. But mY autocomplete doesnot work Help Me
 
Posted by naresh on 10/14/2007 5:40:26 PM
Hi, I am getting undefined values in the div. Please help me Thanks In advance
 
Posted by naresh on 10/14/2007 5:41:49 PM
Hi please send me the vb.net code Thanks
 
Posted by ganapathi on 10/15/2007 5:16:45 AM
Nice article
 
Posted by ganapathi on 10/15/2007 7:12:30 AM
Can you send the code of Autocomplete TextBox using C#
 
Posted by nirut on 10/15/2007 10:12:08 PM
Hi please send me the C#.net code Thanks
 
Posted by Mukesh Mishra on 10/20/2007 2:10:03 PM
Please Help me that how can i get AutoCompleteExtender in asp.net... its urgent
 
Posted by jcuney on 10/22/2007 1:25:35 PM
Can you please provide me with the vb.net code?
 
Posted by Nice on 10/23/2007 6:40:01 AM
Nice Article
 
Posted by Milind on 10/25/2007 1:56:22 PM
hi, How do I obtain the country ID of the selected country instead of the name? I would still like to display the name of the country in the textbox howver, be able to access the ID associated with it either from a hidden field or something upon selection? Do you think it's possible do so? thanks
 
Posted by Antonio on 11/8/2007 10:56:39 AM
How do you hide the AutoComplete when you type a country that not exist in the list?
 
Posted by Anuj on 11/17/2007 7:27:05 AM
i am not getting namespace u mentioned i.e. System.Web.Script.Services.ScriptService in ASp.net2.0 other than that, i typed same code but its not working nethir showing any error
 
Posted by Serdar on 11/18/2007 6:48:29 AM
No VB codes ?
 
Posted by yawar on 11/21/2007 7:35:33 AM
sir how can user this property in desktop windows project.plz help me out... best regards yawar hadi
 
Posted by Adamsakli on 11/28/2007 3:10:21 AM
Thank you very.you are wonderful
 
Posted by Varun Thakur on 11/29/2007 5:12:05 AM
Thank you for giving the solution in such a easy way . I liked the way you present and express the article. Thanx God Bless You
 
Posted by Varun Thakur on 11/29/2007 5:12:18 AM
Thank you for giving the solution in such a easy way . I liked the way you represent and express the article. Thanx God Bless You
 
Posted by rajesh on 12/1/2007 12:52:27 AM
Thank you it help full on my project
 
Posted by rajesh on 12/1/2007 12:53:44 AM
sir, how can i get same thing in vb.net deskop application
 
Posted by rajesh on 12/2/2007 1:07:19 AM
sir, i'm noit able get output in my textbox,my webservice is perfectly working. when i debug services i get XML form on the browser,in aspx page it not shown
 
Posted by Faheem Ahmad on 12/6/2007 1:56:23 AM
Excellent work done. Keep it up to get attention of us. Thanks
 
Posted by ashish on 12/13/2007 5:25:52 AM
can u let me know how do i make one item default selected.
 
Posted by ashish on 12/13/2007 5:26:25 AM