Click here to Skip to main content
15,071,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
i have a textbox and a listbox in my web form ASP.net , what i want is whene i start typing an id on my textbox , the listbox get full of suggestions with ids from my database table that start with the numbers i'm typing
i want only existing ids from database table to appear in listbox
i tried this bau it is not working,the listbox is empty , please help

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
    SqlCommand cmd = new SqlCommand("select num from navire where num = '"+TextBox1.Text+"%'", connect);
             cmd.ExecuteNonQuery();
            SqlDataReader reader = cmd.ExecuteReader();
            // ListBox1.Items.Clear();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    ListBox1.Items.Add(reader.GetString(0).ToString());
                   
                   
                }
            }
        }
Posted
Updated 29-Mar-14 3:23am
v3

if you only display id as a suggestion than try something like this
http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/autocomplete/autocomplete.aspx
   
You really, really, don't want to be doing that!

Think about it: each time your user types a key, a postback has to occur to the server, the server has to create the page, handle the text change, access the database, wait for the response, format it to HTML and send it back to the client. Me, as a user I would have got bored and gone elsewhere by about the third keypress...
Do it in the client! Javascript should be able to handle it: http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-show-textbox-value-exit-in-database-using-javascript/[^] does some of the work.
   
Why.. Why.. Why anyone would do such thing?? It's a bad practice. (Performance issue)

Rather I'd say use AutoComplete Textbox.
See the examples,
AutoComplete With DataBase and AjaxControlToolkit[^]
AutoComplete With DataBase and AjaxControlToolkit[^]
http://www.aspdotnet-suresh.com/2012/08/using-jquery-autocomplete-with-aspnet.html[^]

-KR
   
Comments
Member 10656162 29-Mar-14 20:24pm
   
:) i want to thank you for your solution , my problem is solved
i followed this exemple
http://www.dotnetfox.com/articles/ajax-autocomplete-example-with-database-in-Asp-Net-1079.aspx
ps: i'm just a student and a beginner in asp.net and web app so it really seemed a good solution that's why i tried it

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