How to display corresponding values of combo box in text box?

developer2012
developer2012 used Ask the Experts™
on
Hi there,

I have a vb.net application in which I have a combo box and text boxes.

Combo Box display all employee ID When I select any ID the below text boxes should display the attributes associated with Id. For example, If I select my id 1001 in combo  box then all the text boxes should display my name, last name,  city, phone number  

Any ideas how to achieve it?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
In the event handler for .SelectedIndexChanged for your combobox, do the lookup in your datatable or wherever the information is stored, then populate the other controls with the results of the lookup. Something like:

drFound = dtTable.FindbyID(CInt(cbo.Text))
txtName = drFound.Name
txtCity = drFound.City
etc.
Most Valuable Expert 2012
Top Expert 2014
Commented:
There are two approaches

1) If the number of records is small, you can load a DataTable at the start and keep it in memory. Then you can just look up in the table

Dim EmpID As Integer = cbxEmp.SelectedValue
dTable.DefaultView.RowFilter = "EmpID=" & empID
If dTable.DefaultView.Count = 1 Then
    txtFirstName.Text = dTable.DefaultView.Item(0).Item("FName")
    txtLastName.Text = dTable.DefaultView.Item(0).Item("LName")
    ...
End If


2) If you don't want to keep all records in memory then

Dim EmpID as Integer = cbxEmp.SelectedValue
Dim dbcon As New SqlConnection("connectionString")
dbcon.Open
Dim dbcmd As New SqlCommand("", dbcon)
dbcmd.CommandText = "Select FName, LName ... From Employees Where EmpID=" & EmpID
Dim dbreader As SqlDataReader = dbcmd.ExecuteReader
If dbreader.HasRows Then
    dbreader.Read()
    txtFirstName.Text = dbreader.Item("FName")
    txtLastName.Text = dbreader.Item("LName")
    ...
End If
dbreader.close
dbcmd.dispose
dbcon.dispose

Author

Commented:
@ Code Cruiser. Thanks I got you. But I  think I can not make changes to data it if I am reading through data reader. When the values  appear in the text boxes. I am supposed to edit, delete, or add new record to it. Thanks
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Most Valuable Expert 2012
Top Expert 2014

Commented:
Yes you can't do that with datareader. Given your requirements, I would use a DataGridView instead of combobox and textboxes.
If you don't want to use a DataGridView, but prefer textboxes and similar individual controls for your data, loading everything into a datatable with a DataAdapter then loading a row into the controls as I mentioned above will work for you. Once you're done with updates, you would use the DataAdapter to save the information back to the database.

Author

Commented:
This is how I am loading data in to combo box.

   Private Sub BuildDotCB()
        Dim dsDot As New DataSet
        Try
            DotCB.Items.Clear()
            Dim QueryString As String = "Select Distinct ID FROM tblSystemInformationEmployee "
            Dim drow As DataRow
            drow = Nothing
            dsId = New DataSet
            dsId= GetResultDataset(QueryString, ConnectionString)
            If IsValidDataset(dsDot) Then
                For Each drow In dsId.Tables(0).Rows
                    DotCB.Items.Add(drow(0).ToString())
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window

You could change "Select Distinct ID" to "Select *" and load all the fields from your database into your dataset. Then when an ID is chosen in your combobox, search for the correct record in the table and load. You're not creating a strongly typed table, so you'd use the .Select method of the table, with the field name being the same as you have in the database:

Private Sub DotCB_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DotCB.SelectedIndexChanged
    Dim drFound as DataRow
    drFound = dtTable.Select("ID = " & DotCB.Text)
    txtName.Text = CStr(drFound(1))
    txtCity.Text = CStr(drFound(2))
End Sub

Author

Commented:
I wrote this data view function to manipulate the values as below.
  Private Sub ViewMode(ByVal GUID As String)
        Try

            Dim viewDs As New DataSet()
            Dim Query As String
            Query = "Select * from tblSystemEmployee Where UID = '" & GUID.Trim & "'"
            viewDs = GetResultDataset(Query, True)

            Dim dv As DataView = New DataView(viewDs.Tables(0))
            Dim Notify As String = String.Empty
            If dv.Count > 0 Then

                If Not IsDBNull(dv(0)("UID")) Then
                    txtHWGUID.Text = dv(0)("UID")
                Else
                    txtHWGUID.Text = String.Empty
                End If

              

                If Not IsDBNull(dv(0)("StreetName")) Then
                    txtHWStName.Text = dv(0)("StreetName")
                Else
                    txtHWStName.Text = String.Empty
                End If
            End If
        Catch ex As Exception
            MsgBox("No Values Found")
        End Try
    End Sub

Open in new window


And called the above function on the selectionChangedEvent like this :

Private Sub IDCB_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles IDCB.SelectionChanged
        StreetCB.IsEnabled = False

        ViewMode(IDCB.SelectedItem.ToString)

Open in new window


and When I select any value it show me "No Value found".

Am I missing any thing?

Please help me through this issue.
\
P.S. It is a WPf application.

Thanks!
Since you're getting "No values found", which is the message that is generated by getting an exception, it suggests that either you're getting an error from the query itself, or you're getting a record, but there's a problem in referencing it. Use the debugger to go through line by line. Which line is giving you the error? Offhand, my first thought is that you've specified one of the field names incorrectly (UID or StreetName), but you'd need to find which line is the problem to know for sure.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial