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:
i have a textbox and a listbox in my web form , 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

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

if you only display id as a suggestion than try something like this
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:[^] 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[^][^]

Member 10656162 29-Mar-14 20:24pm
:) i want to thank you for your solution , my problem is solved
i followed this exemple
ps: i'm just a student and a beginner in 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