• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 928
  • Last Modified:

binding dataset to listbox

I am trying to bind a dataset to a list box
   
ListBox1.DataSource = ds.Tables("TableList")
ListBox1.DisplayMember = "TableList.strTableName"
ListBox1.ValueMember = "TableList.strTableName"

ds is the dataset andTableList is the table name.

when i run it i get an error

Cannot create a child list for field TableList

and the dataset just shows rows

System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView

i havent a clue what im doing wrong.

0
AceAdmin
Asked:
AceAdmin
  • 6
  • 4
  • 4
  • +1
1 Solution
 
RonaldBiemansCommented:
ListBox1.DataSource = ds.Tables("TableList")
ListBox1.DisplayMember = "yourcolumnnameyouwanttodisplay"
ListBox1.ValueMember = "yourcolumnnameWhichValueyouwant"

so not the tablename, but a name of a column of your tablelist table


0
 
RonaldBiemansCommented:
so if your tablelist table would contain the columns ID and Name

ListBox1.DataSource = ds.Tables("TableList")
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "ID"


0
 
AceAdminAuthor Commented:
sorry i should have explained further
strTableName is a column name
so i thought that
TableList.strTableName would choose strTableName in table TableList
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Corey ScheichDeveloperCommented:
it is better if you Set the displayMember and ValueMember before attaching the datasource.

ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "ID"
ListBox1.DataSource = ds.Tables("TableList")

I noticed an app acting finnicky by attaching to the data first and then changing the members.  IIRC the ValueMember ended up doing both.

Corey2
0
 
RonaldBiemansCommented:
no because you already supplied the tablename in the datasource
it would work if you had done it this way

ListBox1.DataSource = ds
ListBox1.DisplayMember = "TableList.strTableName"
ListBox1.ValueMember = "TableList.strTableName"

0
 
RonaldBiemansCommented:
But that way isn't prefered.

this is the way it should be done

ListBox1.DisplayMember = "strTableName"
ListBox1.ValueMember = "strTableName"
ListBox1.DataSource = ds.Tables("TableList")

0
 
Corey ScheichDeveloperCommented:
I'll have to take your word for it right now. But here is the PAQ on it.  I was working with RobertRFreeman my origional code is at the top and after I followed Roberts advice there were no more problems.

http://www.experts-Exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21397395.html#13834888
0
 
AceAdminAuthor Commented:
I dont get any errors when i put the DisplayMember and ValueMember before the DataSource but the data is still not diplayed correctly. It is still being show as
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
i havent got any idea why this is happening as it should be showing me the requested column.
0
 
AceAdminAuthor Commented:
I solved the problem by naming the column using the column index
ds.Tables("TableList").Columns(0).ToString
this is now working as wanted
0
 
AceAdminAuthor Commented:
Cheers Corey2 the way you showed me how to solve this problem is not in any documentation that I had.
0
 
TorrwinCommented:
It could be a problem with your dataset, how are you filling your dataset?  (what code?)
0
 
AceAdminAuthor Commented:
just to clarify what I did to resolve this issue

ListBox1.DisplayMember = ds.Tables("TableList").Columns(0).ToString

0
 
Corey ScheichDeveloperCommented:
I am totally confused, did I really show you that?
0
 
AceAdminAuthor Commented:
corey2 you told me to put the displaymember and valuemember before i set the datasource that sorted my error, I figured out that the column name needed to be named via column index number.  
cheers
0
 
Corey ScheichDeveloperCommented:
It should Work without that if you know the String Name for the column.

obj.DisplayMember = "ColumnName"

But don't fix what isn't broken, right?

Thanks for the points though I didn't expect them here, what a nice surprise.
Corey2
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now