Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listbox in my c# application. I want it to display all the table names of the dataset.

What I have tried:

I tried using the listBox.Items.Add(mydataSet().Tables.ToString(); but seems not to work
Posted
Updated 29-Mar-16 1:59am

DataSet.Tables is of type DataTableCollection[^]. If you call ToString() on that, you get the default ToString()-implementation which just gives you the type name.

As DataTableCollection doesn't implement IEnumerable<> (which would allow you to call .Select(table => table.TableName) on it) your only option is to loop over the contained tables (either with a for-loop or a foreach-loop), add the names of the tables to a List<string> and then add the items in that list to your ListBox with .AddRange(..).
 
Share this answer
 
Hi, change your code like this,
C#
for(int i = 0; i < mydataSet.Tables.Count; i++)
{
   listBox.Items.Add(mydataSet.Tables[i].TableName);
}
 
Share this answer
 
v2
Hi Friend,

use this query. this will return you list of tables present in your database .

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
bind this list to your drop down.

try this hope this will help,
thanks..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900