Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Listbox displaying 'System.Data.DataRow'

Posted on 2014-01-26
4
Medium Priority
?
643 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 64

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 64

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 64

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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