954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Combox to populate Textbox

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
verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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")
verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
if consultantcmb.SelectedIndex<>-1 Then
  TextBox1.Text=consultantcmb.SelectedValue
End If
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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.

verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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! :)

verbalurbs
Newbie Poster
15 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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!

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

skran
Light Poster
43 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: