It always go to OleDbException

48 views Asked by At
 using(OleDbConnection con = DAL.GetConnection())
        {
            OleDbTransaction transaction = null;
            try
            {
                con.Open();
                transaction = con.BeginTransaction();

                if (realQuantity == quantity)
                {
                    sql = "DELETE FROM item WHERE (id =" + id + ")";
                }
                else if (realQuantity > quantity)
                {
                    sql = "UPDATE item SET quantity = " + finalQuantity + " WHERE (id = "+id+")";
                }
                OleDbCommand cmd = DAL.GetCommand(con, sql);
                cmd.Transaction = transaction;

                string sql2 = "UPDATE lol SET money = " + finalMoneyBuyer + " WHERE (UserName = '" + Session["username"] + "')";
                OleDbCommand cmd2 = DAL.GetCommand(con, sql2);
                cmd2.Transaction = transaction;

                string sql3 = "UPDATE lol SET money = " + finalMoneySeller + " WHERE (UserName = '" + seller + "')";
                OleDbCommand cmd3 = DAL.GetCommand(con, sql3);
                cmd3.Transaction = transaction;

                int num1 = cmd.ExecuteNonQuery();
                int num2 = cmd2.ExecuteNonQuery();
                int num3 = cmd3.ExecuteNonQuery();

                if(num1 == 0 || num2 == 0 || num3 == 0)
                {
                    //No esperamos a que sea 0, asi que vamos a echar para atras todo lo que hicimos
                    transaction.Rollback();
                    //mandar error
                    Response.Redirect("home.aspx?err=Error1");
                }
                else
                {
                    transaction.Commit();
                    Response.Redirect("home.aspx?err=Purchase was successful!");
                }
            }
            catch(OleDbException ex)
            {
                try
                {
                    //algo malo paso, vamos a echar para atras todo lo que hicimos.
                    transaction.Rollback();
                    Response.Redirect("home.aspx?err=Error2");
                }
                catch{}
            }
        }

I'm showing the code that is necessary, it's not important to know how did I calculate the variables like finalMoneyBuyer or finalQuantity. When I go through this code it does an error in the part of int num2 = cmd2.ExecuteNonQuery();. At the end, it goes to Response.Redirect("home.aspx?err=Error2");. I want that it commit the whole transaction, it doesn't.

I suppose there is a probability that I didn't write good the name of the column in the Database, or an error in the SQL query so I'm trying to show the most things I can so it's easy to you to identify the problem. Thanks!

SQL Query for sql2 after running the WebSite in Microsoft Visual Studio 2013:

sql2

I do have a lol table, a column called money, a column called UserName...

lol table

0

There are 0 answers