Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100