Hello
I am trying to display an ID which is a primary key in my table by selecting the corresponding name in my table. the names have been saved in a combo box and depending on the name selected the textbox will display that Id. the code I have so far only gets the names from sql server but doesnt show the id in the textbox.
can you guide me on this please..

this is the code for combobox. i placed it in the formload

Public Sub RtnCmbVal()
        Dim conn As SqlConnection
        Dim str As String = "select ConsultantId, FullName from Derma_Consultants"
        Dim da As SqlDataReader
        conn = GetConnection()
        Dim ds As New SqlDataAdapter(str, conn)
        Dim cmdGet As New SqlCommand(str, conn)
        conn.Open()
        da = cmdGet.ExecuteReader
        While da.Read
            consultantcmb.Items.Add(da("FullName"))
        End While
    End Sub

Use DataBind.

Public Sub RtnCmbVal()
        Dim conn As SqlConnection
        Dim str As String = "select ConsultantId, FullName from Derma_Consultants"
        
        Dim ds As New SqlDataAdapter(str, conn)
        Dim dt as new DataTable

       ds.Fill(dt)
       
      consultantcmb.DataSource=dt
     consultantcmb.DisplayMember=dt.Columns(1).ColumnName
     consultantcmb.ValueMember=dt.Columns(0).ColumnName
  
    End Sub

Hey I am not too sure how to go about doing this as I have done the following and still the text box is not populated.

ConsultantIdTextBox.DataBindings.Add ("ConsultantId",dset.Tables["Derma_Consultants"],"FullName")
if consultantcmb.SelectedIndex<>-1 Then
  TextBox1.Text=consultantcmb.SelectedValue
End If

Thanks. But I still cant get it to work. will review my code,might be something in there that is stopping the textbox value from being populated.

hey with some extra help the final code is:

Public Sub RtnCmbVal()
        Dim str As String = "select ConsultantId, FullName from Derma_Consultants"
        Dim conn As SqlConnection
        conn = GetConnection()
        conn.Open()
        Dim da As SqlDataAdapter = New SqlDataAdapter(str, conn)
        Dim dt As New DataTable
        da.Fill(dt)
        Dim bs As BindingSource = New BindingSource
        bs.DataSource = dt
        consultantcmb.DataSource = bs
        consultantcmb.DisplayMember = "FullName"
        consultantcmb.ValueMember = "ConsultantId"
        Me.consultantcmb.SelectedValue = -1
    End Sub

 Private Sub consultantcmb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles consultantcmb.SelectedIndexChanged
        If (Not Me.consultantcmb.SelectedValue Is Nothing) Then
            Me.ConsultantIdTextBox.Text = consultantcmb.SelectedValue.ToString
        End If

    End Sub

Thanks for your help! :)

I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question and good luck!

Great job! I m a newbie too and I found this information very helpful!
Thanks a lot!

Hey guys,

Thanks a lot for the above. This has helped me over come my combo / text box issue!

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.