Solved

Listbox filled with system.data.datarowview?????

Posted on 2003-11-18
20
16,186 Views
Last Modified: 2012-08-13
Hi,

I have an application in which I fill a listbox with values from a dataset.  But the listbox is filled with 'system.data.datarowview' although the dataset contains values because I also fill a datagrid with the same dataset and here the values are displayed correctly.

I use the following code:

   Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dataAccess As New ClassDataAccess
        sql = "select maingroupdescription from tblmaingroup"
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter = dataAccess.Getdataadapter(connstring, sql)
        connection.Open()

        Dim dataset1 As New DataSet
        dataadapter.Fill(dataset1)
        Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView

        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "MainGroupDescription"

    End Sub

Can anybody help me with this hair rising problem?

Thanx,

Tom
0
Comment
Question by:zozzie
  • 6
  • 6
  • 3
  • +4
20 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9770835
try to swap the order

       Me.ListBox1.DisplayMember = "MainGroupDescription"
       Me.ListBox1.DataSource = dataset1.Tables(0)
0
 

Author Comment

by:zozzie
ID: 9771104
I tried your suggestion, but it gives the same result.

Is this a bug in VS.NET?

Thanks,

Tom
0
 
LVL 8

Assisted Solution

by:Dranizz
Dranizz earned 50 total points
ID: 9771127
Just point the datasource of the listbox to the dataview


  Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView
  Me.ListBox1.DataSource = dataset1.Tables(0).DefaultView
0
 
LVL 15

Accepted Solution

by:
liebrand earned 50 total points
ID: 9771187
Specify if the value member as well:

        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "MainGroupDescription"
        Me.ListBox1.ValueMember = "MainGroupDescription"
0
 

Author Comment

by:zozzie
ID: 9771318
Thanks for your replies, but

@ Dranizz : tried it, but it gives the same result

@ Liebrand : tried it and it doesn't work : I get an error message:
 'Additional information: Could not bind to the new display member.'

Any other suggestions??

Tom
0
 
LVL 15

Expert Comment

by:liebrand
ID: 9771344
select an additional column in your query:

sql = "select maingroupdescription as displaymember, maingroupdescription as valuemember from tblmaingroup"

then:

me.listbox1.displaymember = "displaymember"
me.listbox1.valuemember = "valuemember"

NOTE: It is case sensative -- if your displaymember and valuemember do not match your column names in your query it will not work.  In most cases if you are using oracle it will uppercase the column names, and I believe SQL keeps what you entered. The best way to see what is being returned to you is to debug the app, and then output the dataset1.getxml() and match your column name casing that way.
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9771347
How are you using the dataview
where do you specified Sort and Filter properties?
0
 

Author Comment

by:zozzie
ID: 9771426
@ Liebrand

Your suggestion works although I left out the .valuemember.

Could you please explain why it has to be coded this way (why do I need an extra statement in the sql-query?

Tom
0
 
LVL 15

Expert Comment

by:liebrand
ID: 9771446
sometimes you might want to display something other than that actual value. For instance, your value might be a customerID...

so you would select customerid as your value member and the customer name as your display member....

your listbox would then SHOW the customer name, but it would use the customer id as the value that gets accessed.

my guess is that it was not the extra statement that did it in the sql, but rather the case of the displaymember value (my guess).
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:zozzie
ID: 9771736
@liebert

I also think it is the displaymember, but now I haveanother problem: suppose I click a MainGroupDescription in the listbox, when I use listbox1.selectedvalue then I suppose I get MainGroupID but this isn't true because when I use the following sql-string for filling up a second listbox:

sql2 = "select groupdescription as displaymember, groupID as valuemember from tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue

it doesn't work because I get an error:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DataRowView' to type 'String' is not valid.


What is the cause of this?

Tom

PS: thanks for your time for helping me
0
 
LVL 15

Expert Comment

by:liebrand
ID: 9771782
post your new code ... i just tested this and it works fine
0
 

Author Comment

by:zozzie
ID: 9771819
OK, here's my code


    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim dataAccess As New ClassDataAccess
        sql = "select maingroupdescription as displaymember, maingroupID as valuemember from tblmaingroup"
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter = dataAccess.Getdataadapter(connstring, sql)
        connection.Open()

        dataset1.Clear()
        dataadapter.Fill(dataset1)
        Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView
        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "displaymember"
        Me.ListBox1.ValueMember = "valuemember"
        connection.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        sql2 = "select groupdescription as displaymember, groupID as valuemember from tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter2 = dataAccess.Getdataadapter(connstring, sql2)
        connection.Open()
        Me.ListBox2.DataSource = dataset1.Tables(2).DefaultView
        Me.ListBox2.DisplayMember = "displaymember"
        Me.ListBox2.ValueMember = "valuemember"
    End Sub
0
 
LVL 15

Expert Comment

by:liebrand
ID: 9771966
Change your SQL to me.listbox1.selectedValue.toString

NOTE: Since you are databinding, your SelectedIndexChanged event is going to be executed when the databind happens. If this is not your desired result, I suggest you do an AddHandler after the .datasource instead of using the Handles on your ListBox1_SelectedIndexChanged

0
 

Author Comment

by:zozzie
ID: 9777107
HI,

I tried your suggestion : "Change your SQL to me.listbox1.selectedValue.toString"

but this doesn't work beacause the method .ToString is not available.  Still I don't understand the problem because the selected value in listbox is an integer just as tblGroup.MainGroupID????

Any ideas?

Thanks  Tom
0
 
LVL 15

Expert Comment

by:liebrand
ID: 9780029
You using Visual Studio .NET 2002 or 2003?
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9781190
The ToString method is available, it just does'nt appear at screen. All objects in énet derived from object class, so th Tostring method is heritate from the object class.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10268127
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: liebrand {http:#9771344} & Dranizz {http:#9781190}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 

Expert Comment

by:pharries
ID: 12397378
Simple answer
I bet that your table contains fields with capitals
Change them all to lower case as VB.net only allows lower case for field names when connecting with databases.
It seems to affect some databases eg (ADS and I gather Oracle) more than others
I spent hours working this out!
so simply rename MainGroupDescription  as maingroupdescription

Hope it helps!
0
 

Expert Comment

by:adsttu
ID: 13429745
I was having this issue an it turned out to be a case-sensitive issue.  The field names in the table, dataset and DisplayMember all have to match case.  Crazy.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now