473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Autocomplete Textbox that Searches from a DataBase

69
Hi Guys,

I cant understand why my textbox is doing nothing. I want a TextBox that would search a database and give suggestions based on what the user types on every keystroke. After doing some research, i realized that you had to use webservice and so that is what i did.
Here is the simple form with a textbox,scriptmanager and an Autocomplete extender
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9.     <title>Untitled Page</title>
  10. </head>
  11. <body>
  12.     <form id="form1" runat="server">
  13.         <asp:ScriptManager ID="ScriptManager1" runat="server">
  14.         <Services>
  15.         <asp:ServiceReference Path="AutoCompleteTest.asmx" />
  16.         </Services>
  17.         </asp:ScriptManager>
  18.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><div>
  19.             <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1" ServiceMethod="GetCentreNumbers" ServicePath="~\AutoCompleteTest.asmx" TargetControlID="TextBox1">
  20.             </cc1:AutoCompleteExtender>
  21.         </div>
  22.     </form>
  23. </body>
  24. </html>
  25.  
  26.  
And here is the webservice
Expand|Select|Wrap|Line Numbers
  1. <%@ WebService Language="C#" Class="AutoCompleteTest" %>
  2.  
  3. using System;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10.  
  11. [System.Web.Script.Services.ScriptService()]
  12. [WebService(Namespace = "http://tempuri.org/")]
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  14. public class AutoCompleteTest  : System.Web.Services.WebService {
  15.     [System.Web.Script.Services.ScriptMethod]
  16.     [WebMethod]
  17.     public string[] GetCentreNumbers(string PrefixText)
  18.     {
  19.          string sql = "Select * from Centre Where centreNo like @PrefixText";
  20.          SqlDataAdapter da = new SqlDataAdapter(sql,ConfigurationManager.ConnectionStrings["DBConnect"].ToString());
  21.          da.SelectCommand.Parameters.Add("@PrefixText", SqlDbType.VarChar, 50).Value = PrefixText+ "%";
  22.          DataTable dt = new DataTable();
  23.          da.Fill(dt);
  24.          string[] items = new string[dt.Rows.Count];
  25.          int i = 0;
  26.          foreach (DataRow dr in dt.Rows)
  27.          {
  28.               items.SetValue(dr["centreNo"].ToString(),i);
  29.               i++;
  30.          }
  31.          return items;
  32.     }
  33.  
  34. }
  35.  
I know the webservice by itself is working becuse i set that as the start page and run the program and invoked the webservice and it gave me the correct answers

Somehow I dont know how the form is calling the webservice because nothing seems to be happening

Can someone please help me?
Apr 5 '08 #1
1 5801
dip_developer
648 Expert 512MB
Hi Guys,

I cant understand why my textbox is doing nothing. I want a TextBox that would search a database and give suggestions based on what the user types on every keystroke. After doing some research, i realized that you had to use webservice and so that is what i did.
Here is the simple form with a textbox,scriptmanager and an Autocomplete extender
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9. <title>Untitled Page</title>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <asp:ScriptManager ID="ScriptManager1" runat="server">
  14. <Services>
  15. <asp:ServiceReference Path="AutoCompleteTest.asmx" />
  16. </Services>
  17. </asp:ScriptManager>
  18. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><div>
  19. <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1" ServiceMethod="GetCentreNumbers" ServicePath="~\AutoCompleteTest.asmx" TargetControlID="TextBox1">
  20. </cc1:AutoCompleteExtender>
  21. </div>
  22. </form>
  23. </body>
  24. </html>
  25.  
  26.  
And here is the webservice
Expand|Select|Wrap|Line Numbers
  1. <%@ WebService Language="C#" Class="AutoCompleteTest" %>
  2.  
  3. using System;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10.  
  11. [System.Web.Script.Services.ScriptService()]
  12. [WebService(Namespace = "http://tempuri.org/")]
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  14. public class AutoCompleteTest : System.Web.Services.WebService {
  15. [System.Web.Script.Services.ScriptMethod]
  16. [WebMethod]
  17. public string[] GetCentreNumbers(string PrefixText)
  18. {
  19. string sql = "Select * from Centre Where centreNo like @PrefixText";
  20. SqlDataAdapter da = new SqlDataAdapter(sql,ConfigurationManager.ConnectionStrings["DBConnect"].ToString());
  21. da.SelectCommand.Parameters.Add("@PrefixText", SqlDbType.VarChar, 50).Value = PrefixText+ "%";
  22. DataTable dt = new DataTable();
  23. da.Fill(dt);
  24. string[] items = new string[dt.Rows.Count];
  25. int i = 0;
  26. foreach (DataRow dr in dt.Rows)
  27. {
  28. items.SetValue(dr["centreNo"].ToString(),i);
  29. i++;
  30. }
  31. return items;
  32. }
  33.  
  34. }
  35.  
I know the webservice by itself is working becuse i set that as the start page and run the program and invoked the webservice and it gave me the correct answers

Somehow I dont know how the form is calling the webservice because nothing seems to be happening

Can someone please help me?
I dont know about AutoCompleteExtender.......

In TextBox1_TextChanged event call the webservice method like
GetCentreNumbers(TextBox1.Text.Trim());.....what happens??
Apr 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: nil | last post by:
Hello all, It's urgent... i want to add autocomplete textbox facility in my application like google. as you type it suggests option to the user..i want the same kind of facility...i know i...
3
shek124
by: shek124 | last post by:
i have a text box on my form. i want to insert the value from the textbox to database.. im the beginner of vb.net .. plz help me .. thanks in advance..
3
by: farahnaz | last post by:
Hi, I really don't know where else to look. I am working on an ajax application and got it working up to a point by referring to numerous websites and learning javascript as I went along. I use a...
2
by: Eric | last post by:
Hi, There is a textbox which contains some text. The purpose is to update the text manually into the textbox and send (update) the updated text to the database. My problem is that when i...
1
by: jagwinderwalia | last post by:
Hi all i have build a autocomplete textbox control that works on keyup event in javascript. everytime a keyup is called , a request is send back to server and the suggestion are displayed in div...
1
by: apoorva varma | last post by:
I want to fetch data into a textbox from database.plz tell me the coding in c#
7
by: Sirajuddin | last post by:
Hi, With great hope im writing this email. I wish and hope someone can help me. Im trying to get code for Autocomplete Textbox. I mean im looking for textbox which will autocomplete textbox...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.