Please I have an autocomplete textbox which fetches value from a tabe in the database. But after i choose the name i want to get the id of the value selected and save it in the database. Am using mysql and c#.

using (MySqlConnection con = new MySqlConnection(MyConString))
            {
                connection.Open();
                string command = "select * from suppliers";
                MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(command, connection);
                MySqlDataReader reader = myCommand.ExecuteReader();

                AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
                while (reader.Read())
                {
                 MyCollection.Add(reader["Name_of_Supplier"].ToString());

                }

                txtSupplier.AutoCompleteCustomSource = MyCollection;

                connection.Close();
            }

Recommended Answers

All 7 Replies

I worry your question was not worded well. Your topic question is "Get id of autocomplete textbox in c# windows form" so in the code provided the id or name of the control appears to be txtsupplier. If that's incorrect, just click once on the object you have on screen in the designer view and get its name/id there.

Sorry @rproffitt . What i mean was after i select the suggested element from the db i have a hidden text box that get filled with the id of the selected element. say txtsup_id. Your help will be really appreciated. Thnx

Still unclear. You state "i have a hidden text box that get filled with the id of the selected element. say txtsup_id." So you have that and you have the name of the source object so the question to me is not any clearer.

look at it like a combo box. which has the id and text(diplaymember and display value). buh this time in a textbox situation

A textbox has an id which you know and the contents which you know. Your question is unclear to me.

If I understand your question correctly, you can use a query like
query = String.Format("select ID from suppliers where Name_of_Supplier = {0}", txtSupplier.Text);
that should return the id

So should i do another query after my first query. Or i should replace this with the my query. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.