Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a combobox attached to an SQL database that pulls the data I need, however I want it to display a different column in that same db row in a textbox based on what was selected in the Combobox.

My combobox is working perfectly displaying all the information I need. Just need to display the 4th column in a textbox based on combobox selection.

DB breakdown: inside my table I have 4 columns:
Name
Direction
number
ipaddress

When select an item in my combobox I want it to display the ipaddress in a textbox named textbox1.


Thank you,

What I have tried:

void Fillcombo()
        {
            string constring = "Data Source=DATABASEHERE;Initial Catalog=HERE;User ID=USERIDHERE;password=PASSWORDHERE";
            string Query = "select * from TABLEHERE;";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand command = new SqlCommand(Query, conDataBase);
            try
            {
                conDataBase.Open();
                SqlDataReader myReader = command.ExecuteReader();

                while (myReader.Read())
                {
                    string lName = myReader["Name"].ToString();
                    string dirName = myReader["Direction"].ToString();
                    string midName = myReader["number"].ToString();
                    comboBox2.Items.Add(lName + " " + dirName + " " + midName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "Data Source=DATABASEHERE;Initial Catalog=HERE;User ID=USERIDHERE;password=PASSWORDHERE";
            string Query = "select * from TABLEHERE where Name='" + comboBox2.SelectedValue + "'";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand command = new SqlCommand(Query, conDataBase);
            try
            {
                conDataBase.Open();
                SqlDataReader myReader = command.ExecuteReader();

                while (myReader.Read())
                {
                    string ipName = myReader["ipaddress"].ToString();
                    textBox1.Text = ipName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 3-Sep-19 5:14am

1 solution

Why are you going back to the database for the "value" when you're already retrieving the "keys"?

You create a "collection" (List<>) with all 4 fields, load the combo box and use the table for lookups; though why all 4 fields can't be in a combo box or grid is a mystery.
 
Share this answer
 
v2
Comments
Member 14576229 3-Sep-19 11:29am    
I am new to C# so still trying to figure out how it all works. All 4 fields could be in the combo box but I want the IP address to display in a text box so I can copy and paste it for easier RDP connections.

I didn't think it was right for the textbox portion to connect back to the DB either but as I said, new to C# and was having a hard time figuring it out.

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