Reputation: 10297
It compiles, it seems to run, but the record is not changed.
Here's my code:
private void UpdateRecord(string ATicketID, string ATicketSource, string AContactsEmail, string AAboutSomeID, string ACategoryID)
{
try
{
con = new OracleConnection(oradb);
con.Open();
String query = "UPDATE ABC.CONCERTTICKETS SET TICKETSOURCE = :p_TICKETSOURCE, ABOUTSOMEID = :p_ABOUTSOMEID, CATEGORYID = :p_CATEGORYID, CONTACTEMAIL = :p_CONTACTEMAIL WHERE TICKETID = :p_TICKETID";
cmd = new OracleCommand(query, con);
cmd.CommandType = CommandType.Text;
OracleParameter p_TICKETID =
new OracleParameter("p_TICKETID", OracleDbType.NVarchar2, ParameterDirection.Input);
p_TICKETID.Size = 20;
p_TICKETID.Value = ATicketID;
cmd.Parameters.Add(p_TICKETID);
OracleParameter p_TICKETSOURCE =
new OracleParameter("p_TICKETSOURCE", OracleDbType.NVarchar2, ParameterDirection.Input);
p_TICKETSOURCE.Size = 20;
p_TICKETSOURCE.Value = ATicketSource;
cmd.Parameters.Add(p_TICKETSOURCE);
OracleParameter p_ABOUTSOMEID =
new OracleParameter("p_ABOUTSOMEID", OracleDbType.Int32, ParameterDirection.Input);
p_ABOUTSOMEID.Value = AAboutSOMEID;
cmd.Parameters.Add(p_ABOUTSOMEID);
OracleParameter p_CATEGORYID =
new OracleParameter("p_CATEGORYID", OracleDbType.Int32, ParameterDirection.Input);
p_CATEGORYID.Value = ACategoryID;
cmd.Parameters.Add(p_CATEGORYID);
OracleParameter p_CONTACTEMAIL =
new OracleParameter("p_CONTACTEMAIL", OracleDbType.NVarchar2, ParameterDirection.Input);
p_CONTACTEMAIL.Size = 100;
p_CONTACTEMAIL.Value = AContactsEmail;
cmd.Parameters.Add(p_CONTACTEMAIL);
try
{
try
{
ot = con.BeginTransaction();
cmd.Transaction = ot;
cmd.ExecuteNonQuery();
ot.Commit();
}
catch (Exception)
{
ot.Rollback();
}
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Apparent success");
}
finally
{
con.Close();
con.Dispose();
}
dataGridView1.Refresh();
}
========= Updated:
So you mean like so, I take it:
try
{
using (var transaction = con.BeginTransaction())
{
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
}
}
catch (Exception ex)
{
ot.Rollback();
throw;
}
MessageBox.Show("Apparent success");
=========== Updated yet again (this code works):
private void UpdateRecord(string ATicketID, string ATicketSource, string AContactsEmail, string AAboutSomeID, string ACategoryID)
{
try
{
con = new OracleConnection(oradb);
con.Open();
String update = @"UPDATE ABC.CONCERTTICKETS
SET TICKETSOURCE = :p_TICKETSOURCE,
ABOUTSOMEID = :p_ABOUTSOMEID,
CATEGORYID = :p_CATEGORYID,
CONTACTEMAIL = :p_CONTACTEMAIL
WHERE TICKETID = :p_TICKETID";
cmd = new OracleCommand(update, con);
cmd.CommandType = CommandType.Text;
// TICKETSOURCE, ABOUTLLSID, CATEGORYID, CONTACTEMAIL, TICKETID
OracleParameter p_TICKETSOURCE =
new OracleParameter("p_TICKETSOURCE", OracleDbType.NVarchar2, ParameterDirection.Input);
p_TICKETSOURCE.Size = 20;
p_TICKETSOURCE.Value = ATicketSource;
cmd.Parameters.Add(p_TICKETSOURCE);
OracleParameter p_ABOUTSOMEID =
new OracleParameter("p_ABOUTSOMEID", OracleDbType.Int32, ParameterDirection.Input);
p_ABOUTSOMEID.Value = AAboutSOMEID;
cmd.Parameters.Add(p_ABOUTSOMEID);
OracleParameter p_CATEGORYID =
new OracleParameter("p_CATEGORYID", OracleDbType.Int32, ParameterDirection.Input);
p_CATEGORYID.Value = ACategoryID;
cmd.Parameters.Add(p_CATEGORYID);
OracleParameter p_CONTACTEMAIL =
new OracleParameter("p_CONTACTEMAIL", OracleDbType.NVarchar2, ParameterDirection.Input);
p_CONTACTEMAIL.Size = 100;
p_CONTACTEMAIL.Value = AContactsEmail;
cmd.Parameters.Add(p_CONTACTEMAIL);
OracleParameter p_TICKETID =
new OracleParameter("p_TICKETID", OracleDbType.NVarchar2, ParameterDirection.Input);
p_TICKETID.Size = 20;
p_TICKETID.Value = ATicketID;
cmd.Parameters.Add(p_TICKETID);
using (var transaction = con.BeginTransaction())
{
try
{
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
MessageBox.Show("Apparent success");
}
finally
{
con.Close();
con.Dispose();
}
Popul8TheGrid();
}
Upvotes: 2
Views: 4000
Reputation: 241789
I note that you really don't have a way to distinguish whether or not an exception was thrown and the transaction was rolled back or if the transaction succeeded. See, you're catching the exception, rolling the transaction back, and then still displaying the message box "Apparent success." because you're swallowing the exception and falling through. There's a reason that people scream from the top of rooftops to not swallow exceptions.
So, I suspect an exception is being thrown, you're catching it, rolling back, and then getting confused because you fall through and show the message box. This is badly written code, and badly written code introduces bugs like this.
At a minimum, I'd rethrow the exception if I were you.
catch (Exception) {
ot.Rollback();
throw;
}
But even better, just wrap the usage of the transaction in a using
block
using(var transaction = con.BeginTransaction()) {
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
}
Why is my update statement not updating the table?
You have a bug in your code. One of these statements
ot = con.BeginTransaction();
cmd.Transaction = ot;
cmd.ExecuteNonQuery();
ot.Commit();
is throwing an exception but you don't know it because you're swallowing all exceptions. Stop swallowing the exceptions, and you'll find out which of these lines is throwing an exception and why. Then you'll have more information with which to debug your underlying issue.
Upvotes: 5
Reputation: 9469
This code rolls back the transaction without informing the user.
catch (Exception)
{
ot.Rollback();
}
Upvotes: 3
Reputation: 839254
Your try/catch blocks are completely broken.
You must fix that first, then you will be able to see what the real error is.
try
{
ot = con.BeginTransaction();
cmd.Transaction = ot;
cmd.ExecuteNonQuery();
ot.Commit();
MessageBox.Show("Success"); // <-- this should be here, not after the catch!
}
catch (Exception)
{
ot.Rollback();
throw; // <-- this is important otherwise the exception is swallowed!
}
Upvotes: 6