I'm calling this proc in C# from oracle. I made the proc to return an error. In other words the proc fails and pv_error is populated with string and the rollback gets triggered, but doesn't work. I'm not sure why. So, what am I doing wrong? Thanks in advance.
private void hhrcv_update_dc_grs_carton()
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
cmd.CommandTimeout = 0;
cmd.CommandText = "dc.hhrcv_update_dc_grs_carton";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("pn_dc_grs_no", OracleDbType.Number).Value = txtDcGRSNo.Text;
cmd.Parameters.Add("pn_pallet_id_no", OracleDbType.Number).Value = txtPalletId.Text;
cmd.Parameters.Add("pn_carton_code", OracleDbType.VarChar).Value = txtCartonCode.Text;
cmd.Parameters.Add("pn_company_id_no", OracleDbType.Number).Value = Companyid;
cmd.Parameters.Add("pn_order_no", OracleDbType.Number).Value = txtOrderno.Text;
cmd.Parameters.Add("pn_emp_id_no", OracleDbType.Number).Value = empid;
cmd.Parameters.Add(new OracleParameter("pv_error", OracleDbType.VarChar));
cmd.Parameters["pv_error"].Direction = ParameterDirection.Output;
string pv_error;
cmd.ExecuteNonQuery();
pv_error = cmd.Parameters["pv_error"].Value.ToString();
if (pv_error.ToString() == "")
{
trans.Commit();
}
else
{
trans.Rollback();
MessageBox.Show("" + pv_error, "Error");
frmReturns r = new frmReturns();
r.Show();
this.Hide();
}
}
Most likely you need to add:
cmd.Transaction = tran;
after calling BeginTransaction
.
Without this the runtime does not know that cmd
is part of the transaction tran
!
For details see the documentation.
As I posted in comment, I Highly reccommend checking connection's Autocommit property.
As Oracle's documentation states
This property determines if Commit is called for the current transaction after the execution of each SQL statement; otherwise, false. The default value is true.
So at least try
conn.Autocommit=false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With