Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everybody

First of all "Please don't judge me I am New In Programming"

i want to add and update some data in database but the form always got Freeze

so i search to solve my problem and i found Background Worker

and i tried to implement the steps that i found in INTERNET but it do nothing and didn't work in wright way i think

What I need

1. disable some Components in the main form

2. showing "Panel Loading" (has progress bar "Marquee" and label and Picture) then adding data when showing the panel and the progrss bar work
My Loading Panel Picture

3. after finishing enable some Components in the main form

if anyone can help me to implement this idea i will be thankful

What I have tried:

C#
private void btnAddHoleInvoiceData_Click(object sender, EventArgs e)
        {
            

            switch (MessageBox.Show(this, "هل تريد بالتأكيد إدخال بيانات الفاتورة (" + it.TempInvoice_Id + ") \"لا يمكن التراجع عن العملية\" ؟", "تأكيد الإدخال", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:

                    #region To Do Befor Adding

                    cmbBxCompanyName.Enabled = false;
                    txtBxInvoiceDescription.Enabled = false;
                    txtBxInvoiceNote.Enabled = false;
                    btnAddNewInvoice.Enabled = false;
                    btnDeleteTempInvoice.Enabled = false;
                    txtBxInvoiceTaxRate.Enabled = false;
                    txtBxInvoiceOther.Enabled = false;
                    txtBxInvoiceDeposit.Enabled = false;

                    cmbBxItems.Enabled = false;
                    txtBxQuantity.Enabled = false;
                    txtBxUnitPrice.Enabled = false;
                    txtBxDiscount.Enabled = false;
                    listView.Enabled = false;
                    btnAddItem.Enabled = false;
                    btnRemoveItem.Enabled = false;
                    lblItemsTotalPrice.Enabled = false;
                    lblInvoiceTotalPrice.Enabled = false;

                    button5.Enabled = false;
                    btnUpdateCustomerData.Enabled = false;
                    btnCancel.Enabled = false;

                    ControlBox = false;

                    pnlLoding.Visible = true;
                    pnlLoding.Refresh();

                    int milliseconds = 4000;
                    Thread.Sleep(milliseconds);

                    #endregion


                    bgWorker.RunWorkerAsync();



                    #region To Do After Adding

                    it.LastInvoice_Id = it.TempInvoice_Id;

                    it.TempInvoice_Id = 0;

                    int milliseconds2 = 4000;
                    Thread.Sleep(milliseconds2);

                    grpBxInvoiceData.Text = " Invoice Data ";

                    foreach (ListViewItem eachItem in listView.Items)
                    {
                        listView.Items.Remove(eachItem);
                    }

                    lblItemsTotalPrice.Text = getItemsSum().ToString("0.00");
                    lblInvoiceTotalPrice.Text = getInvoiceTotalSum().ToString("0.00");

                    txtBxInvoiceDescription.Text = string.Empty;
                    txtBxInvoiceNote.Text = string.Empty;
                    txtBxInvoiceTaxRate.Text = "0.00";
                    txtBxInvoiceOther.Text = "0.00";
                    txtBxInvoiceDeposit.Text = "0.00";

                    cmbBxItems.SelectedIndex = 0;
                    txtBxStoreQuantity.Refresh();
                    txtBxQuantity.Text = string.Empty;
                    txtBxUnitPrice.Text = string.Empty;
                    txtBxDiscount.Text = string.Empty;

                    pnlLoding.Visible = false;

                    cmbBxCompanyName.Enabled = true;
                    txtBxInvoiceDescription.Enabled = true;
                    txtBxInvoiceNote.Enabled = true;
                    btnAddNewInvoice.Enabled = true;
                    btnDeleteTempInvoice.Enabled = false;
                    txtBxInvoiceTaxRate.Enabled = true;
                    txtBxInvoiceOther.Enabled = true;
                    txtBxInvoiceDeposit.Enabled = true;

                    cmbBxItems.Enabled = false;
                    txtBxQuantity.Enabled = false;
                    txtBxUnitPrice.Enabled = false;
                    txtBxDiscount.Enabled = false;
                    listView.Enabled = false;
                    btnAddItem.Enabled = false;
                    btnRemoveItem.Enabled = false;
                    lblItemsTotalPrice.Enabled = true;
                    lblInvoiceTotalPrice.Enabled = true;

                    button5.Enabled = true;
                    btnUpdateCustomerData.Enabled = false;
                    btnCancel.Enabled = true;

                    ControlBox = true;


                    #endregion

                    break;

                case DialogResult.No:

                    return;

                default:

                    break;
            }
        }


Do Work "BackgroundWorker"

C#
private void BgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            int StoreQuantity = 0;

            //try
            //{
                if (listView.Items.Count > 0)
                {

                    dbConnection StartConn = new dbConnection();
                    SQLiteConnection MyConnetion = StartConn.GetConnection();

                    #region Update Invoice Data In Database

                    SQLiteCommand Cm_1 = new SQLiteCommand("Update Invoices set Invoice_Description = @Invoice_Description, Invoice_TaxRate =@Invoice_TaxRate, Invoice_Other = @Invoice_Other, Invoice_Deposit = @Invoice_Deposit, Invoice_DetailTotal = @Invoice_DetailTotal, Invoice_InvoiceTotal = @Invoice_InvoiceTotal, Invoice_Note = @Invoice_Note where Invoice_Id = @Invoice_Id", MyConnetion);

                    Cm_1.Parameters.Add("@Invoice_Id", it.TempInvoice_Id.ToString());
                    Cm_1.Parameters.Add("@Invoice_Description", txtBxInvoiceDescription.Text);
                    Cm_1.Parameters.Add("@Invoice_TaxRate", txtBxInvoiceTaxRate.Text);
                    Cm_1.Parameters.Add("@Invoice_Other", txtBxInvoiceOther.Text);
                    Cm_1.Parameters.Add("@Invoice_Deposit", txtBxInvoiceDeposit.Text);
                    Cm_1.Parameters.Add("@Invoice_DetailTotal", lblItemsTotalPrice.Text);
                    Cm_1.Parameters.Add("@Invoice_InvoiceTotal", lblInvoiceTotalPrice.Text);
                    Cm_1.Parameters.Add("@Invoice_Note", txtBxInvoiceNote.Text);

                    MyConnetion.Open();

                    Cm_1.ExecuteNonQuery();

                    MyConnetion.Close();

                    #endregion


                    #region Insert Invoice Items

                    for (var i = 0; i < listView.Items.Count; i++)
                    {

                        SQLiteCommand Cm_2 = new SQLiteCommand("insert into InvoiceDetails (Invoice_Id,Item_Id,Item_Description,Item_NeededQuantity,Item_UnitPrice,Item_Discount,Item_Total) " +
                                    "values (@Invoice_Id,@Item_Id,@Item_Description,@Item_NeededQuantity,@Item_UnitPrice,@Item_Discount,@Item_Total)", MyConnetion);

                        Cm_2.Parameters.Add("@Invoice_Id", it.TempInvoice_Id.ToString());
                        Cm_2.Parameters.Add("@Item_Id", listView.Items[i].SubItems[0].Text);
                        Cm_2.Parameters.Add("@Item_Description", listView.Items[i].SubItems[1].Text);
                        Cm_2.Parameters.Add("@Item_NeededQuantity", listView.Items[i].SubItems[2].Text);
                        Cm_2.Parameters.Add("@Item_UnitPrice", listView.Items[i].SubItems[3].Text);
                        Cm_2.Parameters.Add("@Item_Discount", listView.Items[i].SubItems[4].Text);
                        Cm_2.Parameters.Add("@Item_Total", listView.Items[i].SubItems[5].Text);

                        MyConnetion.Open();

                        Cm_2.ExecuteNonQuery();

                        MyConnetion.Close();

                    }

                    #endregion

                    #region Update Items Quantity In Database

                    for (var i = 0; i < listView.Items.Count; i++)
                    {
                        SQLiteCommand Cm_3 = new SQLiteCommand(" select Item_StoreQuantity from Items where Item_Id = '" + listView.Items[i].SubItems[0].Text + "'", MyConnetion);

                        MyConnetion.Open();

                        int count = Convert.ToInt32(Cm_3.ExecuteScalar());

                        SQLiteDataReader dr = Cm_3.ExecuteReader();

                        dr.Read();

                        if (count != 0)
                        {
                            StoreQuantity = Convert.ToInt32(dr["Item_StoreQuantity"]);


                            MyConnetion.Close();

                        }
                        else
                        {

                        }

                        int updateStoreQuantity = StoreQuantity - Convert.ToInt32(listView.Items[i].SubItems[2].Text);

                        SQLiteCommand Cm_4 = new SQLiteCommand("Update Items set Item_StoreQuantity = @Item_StoreQuantity where Item_Id = @Item_Id", MyConnetion);

                        Cm_4.Parameters.Add("@Item_Id", listView.Items[i].SubItems[0].Text);
                        Cm_4.Parameters.Add("@Item_StoreQuantity", updateStoreQuantity.ToString());

                        MyConnetion.Open();

                        Cm_4.ExecuteNonQuery();

                        MyConnetion.Close();

                    }

                    #endregion

                }
                else
                {

                }
}
Posted
Updated 7-May-19 15:40pm
Comments
Christian Graus 7-May-19 19:51pm    
Instead of posting 10000 lines, post the bit that does what you're talking about. Do you know how to use your debugger? What does 'not working' mean? Is your background code called? What does it do?
Eng Mohamed Bassuny 8-May-19 10:13am    
sorry about that .. i thought this will be better if i put the two events and i think i put exactly what i talking about

i should see in the database

1. Update Invoice
2. Insert Invoice Items
3. Update Items Quantity In Database

but what i got is update Invoice only in the database


i called backgroung >>>bgWorker.RunWorkerAsync();

it do all the second code i write >>> Do Work "BackgroundWorker"

this is my project
https://drive.google.com/file/d/13W7OdFR9mR1qQzFdu3shuiYgy-CmJ2aY/view?usp=sharing

and i use it because when i put all this code in button it hang the UI
and i search for some help and i found that backgroungworker will be the way to solve that

so if you can help me and tell me how to do that i will be thankful
Christian Graus 7-May-19 20:04pm    
Also an async method should be called with an await keyword, which requires making the method async
BillWoodruff 8-May-19 0:01am    
Start by telling us exactly what went wrong in your first effort: what was the error message; what values did you see that were not expected, or in error ?
Eng Mohamed Bassuny 8-May-19 10:07am    
i put Background Worker to avoid crash the UI but when i use it it didn't anything and the progress bar hanging

and i should see in the database

1. Update Invoice
2. Insert Invoice Items
3. Update Items Quantity In Database

but what i got is update Invoice only in the database

i didn't get any error massage or any exceptions so i decided to ask for help

this is my project
https://drive.google.com/file/d/13W7OdFR9mR1qQzFdu3shuiYgy-CmJ2aY/view?usp=sharing

1 solution

I've written an article on SQLite that shows how to work with both synchronous and asynchronous methods that should answer all of your questions: Working with SQLite in C# & VB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900