Пример #1
0
        private void btnCheckout_Click(object sender, EventArgs e)
        {
            if (grdDataCart.RowCount == 0)
            {
                MessageBox.Show("There is nothing in the shopping Cart");
            }
            else
            {
                int    OrderId    = Order.getNextOrderId();
                int    SupplierId = Convert.ToInt16(label8.Text);
                string Status     = "O";
                float  total      = float.Parse(txtBalance.Text);
                //float OriginalBalance = float.Parse(grdDataSupp.Rows[grdDataStock.CurrentCell.RowIndex].Cells[2].Value);

                DateTime dt   = DateTime.Now;
                string   date = dt.ToString("dd-MMM-yyyy");

                using (OracleConnection connection = new OracleConnection(DBConnect.oradb))
                {
                    connection.Open();

                    OracleCommand     command = connection.CreateCommand();
                    OracleTransaction transaction;

                    // Start a local transaction.
                    transaction = connection.BeginTransaction();

                    // Must assign both transaction object and connection
                    // to Command object for a pending local transaction
                    command.Connection  = connection;
                    command.Transaction = transaction;

                    try
                    {
                        //microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.begintransaction?view=netframework-4.7.2


                        command.CommandText =
                            "Insert into Orders VALUES (" + OrderId + ",'" + date + "'," + SupplierId + "," + total + ",'" + Status + "')";
                        command.ExecuteNonQuery();


                        float currentBalance = float.Parse(grdDataSupp.Rows[grdDataSupp.CurrentCell.RowIndex].Cells[2].Value.ToString());
                        float newBalance     = currentBalance + total;



                        for (int i = 0; i < grdDataCart.RowCount; i++)
                        {
                            int   StockId  = Convert.ToInt16(grdDataCart.Rows[i].Cells[1].Value.ToString());
                            float price    = float.Parse(grdDataCart.Rows[i].Cells[3].Value.ToString());
                            int   quantity = Convert.ToInt16(grdDataCart.Rows[i].Cells[4].Value.ToString());

                            command.CommandText =
                                "INSERT INTO OrderItems VALUES(" + OrderId + "," + StockId + "," + price + "," + quantity + ",'O',0)";
                            command.ExecuteNonQuery();
                        }

                        command.CommandText =
                            "UPDATE Supplier SET Balance = " + newBalance + "where SupplierId = " + SupplierId;
                        command.ExecuteNonQuery();

                        // Attempt to commit the transaction.
                        transaction.Commit();

                        txtAmountOrder.Value = 0;
                        grdDataCart.Rows.Clear();
                        grdDataCart.Hide();

                        txtPrice.Clear();
                        txtBalance.Clear();
                        grpCart.Hide();
                        grpAddCart.Hide();
                        grpStockSelection.Hide();

                        DataSet ds = new DataSet();
                        grdDataSupp.DataSource = Supplier.getSupplierSummary(ds).Tables["stk"];

                        grpSupplier.Show();
                    }


                    catch (Exception ex)
                    {
                        Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
                        Console.WriteLine("  Message: {0}", ex.Message);

                        // Attempt to roll back the transaction.
                        try
                        {
                            transaction.Rollback();
                        }
                        catch (Exception ex2)
                        {
                            // This catch block will handle any errors that may have occurred
                            // on the server that would cause the rollback to fail, such as
                            // a closed connection.
                            Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                            Console.WriteLine("  Message: {0}", ex2.Message);
                        }
                    }
                }
            }
        }