Sadiq
Sadiq

Reputation: 838

How to retrieve data from DataSet to ListBox?

All I am getting is the empty listBox while retrieving data from DataSet. Heres the code:

DataSet dt_product = dBCommand.ExecuteNonQuery("SELECT * FROM PRODUCT");

listBx_prod.DataSource=dt_product.Tables[0].Columns[1].ExtendedProperties.Cast<DataRow>().ToList();      

I debugged and my DataSet successfully retrieves data from Database. Where am I wrong?

Upvotes: 1

Views: 1082

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460058

I must admit that i don't know why you are trying to read the PropertyCollection from the second DataColumn in your first table of the DataSet via ExtendedProperties at all.

But since you've mentioned that you just want to show the data in the ListBox:

listBx_prod.DataSource = dt_product.Tables[0];
listBx_prod.DisplayMember = "ProductName";
listBx_prod.ValueMember = "ProductId";

Upvotes: 2

kmatyaszek
kmatyaszek

Reputation: 19296

You should set DataSource to DataTable and set ValueMember and DisplayMember:

listBox1.DataSource = dt_product.Tables[0];
listBox1.ValueMember = "Id";
listBox1.DisplayMember = "Name";

Upvotes: 1

Related Questions