?
Solved

Listbox displaying 'System.Data.DataRow'

Posted on 2014-01-26
4
Medium Priority
?
628 Views
Last Modified: 2014-01-27
I have two listboxes on a form representing two tables from a SQL Server database in a parent-child relationship.

The second listbox populates on clicking the first listbox, but although listbox2 shows the correct number of rows, the correct values are not displayed, just 'System.Data.DataRow'.

 Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
        Dim rows() As DataRow = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2")
        Me.lbxTwo.DataSource = rows
    End Sub

Open in new window

0
Comment
Question by:TimHudspith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39810127
Hi TimHudspith;

Although you have connected the records to be associated with the second ListBox you have not told it which column to to display in it. Please see the code snippet and change the string ColumnName to a string name of the actual column name to be displayed.

Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
    Dim rows() As DataRow = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2")
    Me.lbxTwo.DataSource = rows
    Me.lbxTwo.DisplayMember = "ColumnName"    
End Sub

Open in new window

0
 

Author Comment

by:TimHudspith
ID: 39810246
I've done that but it makes no difference. Strangely, I can actually set DisplayMember to a column name that even doesn't exist and there's no exception thrown.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39810270
Hi TimHudspith;

Setting the DisplayMember property incorrectly will NOT throw an exception but will use the objects ToString method to supply the string and the reason why you get  "System.Data.DataRow' as the object being displayed.

When you typed the name as a string did you use the same case for all the characters?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39810581
Hi TimHudspith;

I have looked at the code again and did some testing and found out that the ListBox.DataMember is not going to work in this case because the binding to the DataRow collection is to complex. So I came up with an alternative solution. In the code snippet below I made changes to make it work. What you need to do is the following:

In this part of the statement, c.Field(Of String) change String to the data type of the data held by the column you wish to display, if the data in that column is of type String then you do not need to do anything as it is already String. If it is of another data type for example type Integer then change String to Integer and also add .ToString() to the end of this part of the statement, c.Field(Of String)("ColumnName") like so c.Field(Of String)("ColumnName").ToString()

In this part of the statement, ("ColumnName"), change "ColumnName" to the actual column name in the DataRow.

That is all the changes that is needed.

Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
    Dim rows As List(Of String) = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2").Select(Function(c) c.Field(Of String)("ColumnName")).ToList()
    Me.lbxTwo.DataSource = rows
End Sub

Open in new window

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question