Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocomplete textbox from the database

Tags:

c#

I need my textbox to autocomplete when the user types. The value should come from the database. I am using the textchange property of the textbox.

protected void autocomplete(object sender, EventArgs e)
{
    string query = "Select area,codes from tbl_pincode";
    SqlConnection conn = new SqlConnection("Data Source=win2008-2;Initial       Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;");
    SqlCommand com = new SqlCommand(query, conn);
    conn.Open();
    SqlDataReader dr = com.ExecuteReader();
    while (dr.Read())
    {
        zipcode.Text = dr.GetValue(0).ToString();
    }
    conn.Close();
}

But i m not getting the desired result. Any ideas how to go about it?

like image 288
asifa Avatar asked Jan 19 '12 12:01

asifa


People also ask

How can create AutoComplete TextBox from database in jQuery?

Add web form right click on project add new item and choose web form. Add script and styles cdn link in head section. Design HTML by grad and drop textbox control in web form. Complete html code of web form.

How do you make a TextBox Autosuggested in Windows Forms?

Append displays first value of the suggestion appended or selected in the TextBox, other values can be navigated using arrow keys. SuggestAppend displays suggested values as dropdown and first value appended in the TextBox. AutoCompleteSource property sets the source for auto complete data.


3 Answers

Have you looked at using the jquery ui autocomplete component? You can hook this up to a remote datasource

http://jqueryui.com/demos/autocomplete/

like image 145
mbx-mbx Avatar answered Sep 25 '22 23:09

mbx-mbx


You can use jQuery UI for autocomplete: http://www.dotnetcurry.com/ShowArticle.aspx?ID=515

Another option for ASP.NET autocomplete is AjaxControlToolkit: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx

like image 35
Fabio Avatar answered Sep 25 '22 23:09

Fabio


  1. Never put your connection strings on ANY forum
  2. Use AJAX otherwise you'll have to postback the page every time the user types a character. JQuery & JQueryUI provide easy support for autocomplete features.
  3. use Telerik RadCombo (but you need to buy a license)

EDIT:

If you choose to use the JQueryUI autocomplete, I'd start from the remote JSONP example. You can point the url property of the ajax call inside the autocomplete's source function to a WebMethod in your page. This will receive an object containing the filter (data in the example) and return the required values from your database in JSON format (e.g. see this example)

like image 45
Strillo Avatar answered Sep 22 '22 23:09

Strillo