Link to home
Start Free TrialLog in
Avatar of kumarsundaram
kumarsundaramFlag for Canada

asked on

Navigating data in related tables

Hello All,
   I am in the process of learning VB.NET and ADO.NET.  I am working on an application that requires relationship between tables.

Now I have a relationship setup like this:
        tableRelation = New DataRelation("relation", dsTest.Tables("clients").Columns("clientID"), dsTest.Tables("tasks").Columns("clientID"))

And, I added that relationship to my dataset as follows:
         dsTest.Relations.Add(tableRelation)


Now, I have a list box and a text box. The idea is to display the clientName (from my clients table as you can see in the relationship above) on my listbox. And, when the user selects a clientname from the list box I want the client's phone number to be displayed on the text box (example setup I have now for testing purposes).

So, I populate the listbox like this:
        For Each rowParent In dsTest.Tables("clients").Rows
            ListBox1.Items.Add(rowParent("clientName"))
        Next
And that's working beautifully and displays the all the client names in the list box.

NOW, THIS IS WHERE MY PROBLEM BEGINS:-)

The code that I have currently is as follows in the listbox1_selectedIndexChanged control:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim task As String
        Dim selectedClient As DataRow
        Dim selectedColumn As DataColumn
        Dim drArray As DataRow()

        drArray = rowParent.GetChildRows("tableRelation")
        For Each selectedClient In drArray
                For Each selectedColumn In selectedClient.Table.Columns
                     task = selectedClient("taskName")
                Next
        Label1.Text = task
        Next
 End Sub


Now, Of course this doesn't work:-(  I don't know how else to do it. Could you please help me pointing in the right direction on how I can accomplish displaying related data in listbox and a textbox as I described in the beginning....   If my code is entirely wrong can you provide me some sample code to get started??? Thanks a lot in advance.
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kumarsundaram

ASKER

AW,
   Thanks for pointing out the relationship name... It *kind of* works now but not quite what I wanted it to...
   When I select a name (clientname in this case) in the listbox, It does display something on the label. But, the label isn't getting updated everytime I select a different client name on the list box. I tried moving the line "label1.text = task" to different areas of the nested loop. It doesn't do the trick.
   I feel like my code is missing something for it to update the text in the label  everytime I choose a different name in the listbox(displaying the task name for selected client name)

Any thoughts???

Thanks
have you set a breakpoint in the code, and then stepped through the code, to see what is happening, when?

AW