I am currently trying to add data from an SQLite database and this is where I am at. The amount of data values in the combo box is correct, but it is not showing the actual data value, just ListViewItem: {}, please can someone show me where I have gone wrong.
SQLiteConnection techWorldConnection = new SQLiteConnection(@"Data Source=C:\Users\mwbel\OneDrive\Homework\A Level\Comp Science\NEA\NEA Program\NEA Example\bin\Debug\TechWorld.db"); // directs code to location of my database file
techWorldConnection.Open(); // goes to my database file and reads it
using (SQLiteConnection conn = new SQLiteConnection(techWorldConnection))
{
SQLiteDataAdapter sda = new SQLiteDataAdapter("Select productName From Products", conn);
DataSet dataset = new DataSet();
sda.Fill(dataset);
foreach (DataRow row in dataset.Tables[0].Rows)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(row["productName"].ToString());
OrderProductList.Items.Add(lvi);
}
}
techWorldConnection.Close();
CodePudding user response:
The amount of data values in the combo box is correct, but it is not showing the actual data value, just ListViewItem: {}
The issue is how you're adding the items, specifically this area:
foreach (DataRow row in dataset.Tables[0].Rows)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(row["productName"].ToString());
OrderProductList.Items.Add(lvi);
}
You don't need to loop the collection to get the data you need, you can fill a DataTable
, set the DisplayMember
on the ComboBox
and finally set the DataSource
of the ComboBox
to your datatable.
OrderProductList.DisplayMember = "productName";
SQLiteDataAdapter sda = new SQLiteDataAdapter("Select productName From Products", conn);
DataTable table = new DataTable();
sda.Fill(table);
OrderProductList.DataSource = table;