In my windows form, I have a combo box which is populated with (part code)values from my database and I'm finding it hard to set each equivalent (part name)value to a text box which is in the same table. I was able to populate combo box values using the code below but I cannot work on text box field. Can anyone help with this? Thank you in advance. :)

If cmbProcess.SelectedIndex = "0" Then
            Try
                con.Open()
                Dim strSQL As String
                strSQL = "select PartCode from tblINKPROCESSING"
                Dim da As New SqlDataAdapter(strSQL, con)
                Dim ds As New DataSet
                da.Fill(ds, "tblINKPROCESSING")

                With cmbPCode
                    .DataSource = ds.Tables("tblINKPROCESSING")
                    .DisplayMember = "PartCode"
                    .ValueMember = "PartCode"
                    .SelectedIndex = -1
                End With
            Catch ex As Exception
                MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
            Finally
                con.Close()
            End Try
        End If

Dani AI

Generated

Nice work getting this working, . Building on ’s approach, you can avoid a round trip to SQL on every selection by loading both columns once and using simple WinForms data binding so the text box updates automatically when the user picks a part code.

' Load both code and name once (e.g., in Form_Load)
Dim table As New DataTable()
Using cn As New SqlConnection(connectionString),
      da As New SqlDataAdapter("SELECT PartCode, PartName FROM tblINKPROCESSING ORDER BY PartCode", cn)
    da.Fill(table)
End Using

' Bind the combo to the table
cmbPCode.DataSource = table
cmbPCode.DisplayMember = "PartCode"   ' what the user sees
cmbPCode.ValueMember = "PartName"     ' what SelectedValue returns
cmbPCode.DropDownStyle = ComboBoxStyle.DropDownList
cmbPCode.SelectedIndex = -1

' Bind the textbox to the combo's SelectedValue (no extra queries)
txtPName.DataBindings.Clear()
txtPName.DataBindings.Add("Text", cmbPCode, "SelectedValue", True, DataSourceUpdateMode.Never, "")

Tips:

  • If PartName can be NULL in the database, the "" default in the binding keeps the text box empty instead of showing Nothing.
  • Prefer SelectionChangeCommitted if you later handle user changes explicitly; it will not fire during data binding setup.
  • Keep ’s point in mind: SelectedIndex is an Integer. Compare to numbers (0, -1), not strings.
  • If PartCode is not unique, fix that at the data level or include the primary key in your query and bind to a BindingSource.

This pattern keeps the UI responsive, reduces database calls, and makes the code easier to maintain.

Recommended Answers

All 3 Replies

Hi

I am assuming that PartCode is unique and that when you want to get the PartName, this will occur when you choose a PartCode from the combo box?

If the assumption above is correct, then you could do something like the following in the SelectedIndexChanged event of the combo box:

Dim selectStatement As String = "SELECT PartName FROM tblINKPROCESSING WHERE PartCode = @PartCode"

Using connection As New SqlConnection(connectionString)

    connection.Open()

    Using Command As New SqlCommand(selectStatement, connection)

        Command.Parameters.AddWithValue("@PartCode", cmbPCode.Text)

        Dim reader As SqlDataReader = Command.ExecuteReader

        Do While reader.Read()

            txtPName.Text = reader("PartName").ToString

        Loop

    End Using

End Using

HTH

You are going to have a problem with

If cmbProcess.SelectedIndex = "0" Then

because cmbProcess.SelectedIndex is numeric and you are comparing it to a string. Try

If cmbProcess.SelectedIndex = 0 Then

Got it! It worked.

Djjeavons Thank you very much for your help! Much appreciated. :) :)

Reverend Jim .. thank you also for sharing some points to consider. :)

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.