开发者

Checking for existing item isn't working

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, "U开发者_如何学运维h, oohhhhhh!");
            }
            msc.Close();

            this.Cursor = Cursors.Arrow;


The problem is here:

 if (thelist.Items.Count == 0 || 
     !thelist.Items[0].Text.Contains(msdr[0].ToString()))

In your if condition you are only comparing with the text of the first item in the listview - you should check all of them.

You could do something like this:

bool hasItem = false;
foreach (ListViewItem item in thelist.Items)
    if (item.Text.Contains(msdr[0].ToString())) hasItem = true;

if (!hasItem)
{
    //...
}


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:

 if (thelist.Items.Count == 0 || !thelist.Items[0].Text.Contains(msdr[0].ToString()))

you need to loop through the entire list. Something like this perhaps (not tested)?

 var found = false;
 foreach (var item in thelist.Items)
 {
   if (item.Text.Contains(msdr[0].ToString())
   {
     found = true;
     break;
   }
 }
 if (thelist.Items.Count == 0 || !found)
 {
   ....


To fix your query, you need to add a GROUP BY.

string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, phone, company FROM mysql_9269_dbase.order" GROUP BY sessionid;

That should take care of that part.


So, thanks to @CodingInsomnia and @BrokenGlass, here's the entire snippet that works:

// 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)
                    {
                        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();
                    }
                    else
                    {
                        var found = false; foreach (var item in thelist.Items)
                        {
                            if (item.ToString().Contains(msdr[0].ToString()))
                                found = true;
                        }
                        if (thelist.Items.Count == 0 || !found)
                        {
                            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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜