In my winforms app, I have a list view control that is populated with new order information.
I am using DISTINCT(something) to get only 1 of each thing in the database table, and I am also doing a check in C# so that if it is already in the listview control, then not to add it.
But it juust keeps adding junk from the table that is already in the listview control, and it is also NOT obeying the DISTINCT command!
Here is my code, any help at all is really appreciated, thanks:
// Check for new orders.
MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
try
{
this.Cursor = Cursors.WaitCursor;
msc.Open();
// Check for orders now.
string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, phone, company FROM mysql_9269_dbase.order";
MySql.Data.MySqlClient.MySqlCommand cd = new MySql.Data.MySqlClient.MySqlCommand(st, msc);
MySql.Data.MySqlClient.MySqlDataReader msdr = cd.ExecuteReader();
while (msdr.Read())
{
if (thelist.Items.Count == 0 || !thelist.Items[0].Text.Contains(msdr[0].ToString()))
{
ListViewItem LItem = new ListViewItem(msdr[0].ToString());
ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);
SubItems.Add(msdr[1].ToString());
SubItems.Add(msdr[2].ToString());
SubItems.Add(msdr[3].ToString());
SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
SubItems.Add(msdr[8].ToString());
SubItems.Add(msdr[9].ToString());
thelist.Items.Add(LItem);
thelist.Update();
}
}
}
catch (Exception en)
{
MessageBox.Show(en.Message, "Uh, oohhhhhh!");
}
msc.Close();
this.Cursor = Cursors.Arrow;
Not sure about the database stuff, but it seems you are only checking if the FIRST element in your listbox is the same as the one you are about to add:
you need to loop through the entire list. Something like this perhaps (not tested)?