开发者

Can't get info from MySQL Database from inside DoWork()

开发者 https://www.devze.com 2023-02-17 17:35 出处:网络
I am using Background worker, and I have tested this MySQL/C# select query OUTside of the backgroundworker class, and it works perfectly. But when I try to use backgroundworker with it, all the string

I am using Background worker, and I have tested this MySQL/C# select query OUTside of the backgroundworker class, and it works perfectly. But when I try to use backgroundworker with it, all the string values are empty at the end of it!

    void bWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (action != Action.ViewOrder)
        {
            if (HasErrors != null || HasErrors != "" || HasErrors != String.Empty)
            {
                Alert(HasErrors,
                    "Retry",
                    Error,
                    true,
                    Color.FromArgb(249, 87, 55)
                );
            }
            else
            {
                var found = false;

                foreach (var item in thelist.Items)
                {
                    if (item.ToString().Contains(OrderNumber))
                        found = true;
                }
                if (thelist.Items.Count == 0)
                {
                    ListViewItem LItem = new ListViewItem(OrderNumber);
                    ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);

                    SubItems.Add(FirstName + " " + LastName);
                    SubItems.Add(EmailAddress);
                    SubItems.Add(DeliveryAddress);
                    SubItems.Add(Company);
                    SubItems.Add(PhoneNumber);

                    thelist.Items.Add(LItem);
                    thelist.Update();
                }
            }
        }
    }

    void bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        SlideTimer.Enabled = true;
        Alert("All unconfirmed orders have been retrieved.",
            "Dismiss",
            Information,
            true,
            Color.FromArgb(63, 187, 249)
        );
        action = Action.None;
    }


    void bWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; (i <= 10); i++)
        {
            if (bWorker.CancellationPending == true)
            {
                e.Cancel = true;
            }
            els开发者_Go百科e
            {
                if (action == Action.ViewOrder)
                {
                    // Connect to Database, check for orders, and end.
                            string cs = "server=" +
                                Settings.Default.Server +
                                ";username=" +
                                Settings.Default.Username +
                                ";database=" +
                                Settings.Default.Database +
                                ";port=3306;password=BLAH" +
                            ";";
                    MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
                    try
                    {
                        msc.Open();

                        // Check for new orders.
                        string st = "SELECT sessionid, firstname, lastname, email, streetaddress, suburb, postcode, state, company, phone 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())
                        {
                            OrderNumber = msdr[0].ToString();
                            FirstName = msdr[1].ToString();
                            LastName = msdr[2].ToString();
                            EmailAddress = msdr[3].ToString();
                            DeliveryAddress = msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7];
                            Company = msdr[8].ToString();
                            PhoneNumber = msdr[9].ToString();


                           // System.Threading.Thread.Sleep(500);
                            bWorker.ReportProgress((i * 10));
                        }

                        msc.Close();
                    }
                    catch (Exception en)
                    {
                        HasErrors = en.Message;
                    }

                }
            }
        }
    }

I know there is information in the table, and I also know there's nothing wrong with the mysql query, as it works if i don't use backgroundworker.

Why won't it work with BackgroundWorker?


I see two major problems that are going to prevent your list from being updated. The first is the if statement on the HasErrors string in bWorker_ProgressChanged. When you OR those conditions together, you might as well be saying if(true) because every string will be either not null OR not empty. I think you can accomplish what you're going for with:

if(!string.IsNullOrEmpty(HasErrors))

The other problem is that thelist is only going to be empty the first time the function runs. You need to change if (thelist.Items.Count == 0) to if (!found).

0

精彩评论

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