Note that there are some explanatory texts on larger screens.

plurals
  1. POCode That Only Executes if Try Does Not Catch
    text
    copied!<p>On a button click I am saving info to a Sqlite database. I have the command.ExecuteNonQuery() in a try block. I have got everything handled just find if the catch block is caught, but if everything makes it through just fine I want other code to execute that would clear out the values of my EditTexts and set focus. I try putting that code after the ExecuteNonQuery() in my try block, but it still executes before the catch block even if an exception is caught so the values of my edittexts get cleared out before the catch block can even do anything. Same story if I add the code after my try/catch block entirely. The catch block seems to be the last thing executing and by then the values have been cleared and the catch block can't even execute properly. How do I set the values to clear only after the catch block is cleared and no exceptions are thrown?</p> <p>EDIT: Tried putting it in a finally block but the same thing. Locals window shows both partnumber.Text and partQty.text are blank by the time they get to the catch block. But if I take out the code that clears those fields then both still have their values in the catch block. Is there something special maybe about Sqlite exceptions that would create a timing issue?</p> <pre><code> try { c.ExecuteNonQuery(); partnumber.Text = ""; partqty.Text = ""; partnumber.RequestFocus(); } catch (SqliteException ex) { if (ex.ErrorCode.ToString() == "Constraint") { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetTitle("Item Duplication"); builder.SetMessage("You have already counted this item. How would you like to proceed?"); builder.SetPositiveButton("Add to Previous", delegate { var newQty = Convert.ToInt32(test.currQuantity(partnumber.Text)) + Convert.ToInt32(partqty.Text); var n = connection.CreateCommand(); connection.Open(); n.CommandText = "Update [Items] Set Quantity = '" + newQty.ToString() + "' Where ItemNmbr = '" + partnumber.Text + "'"; n.ExecuteNonQuery(); Toast.MakeText(this, "Quantity updated to: " + newQty.ToString(), ToastLength.Long) .Show(); partnumber.Text = ""; partqty.Text = ""; partnumber.RequestFocus(); connection.Close(); return; }); builder.SetNegativeButton("Override Previous", delegate { var n = connection.CreateCommand(); connection.Open(); n.CommandText = "Update [Items] Set Quantity = '" + partqty.Text + "' Where ItemNmbr = '" + partnumber.Text + "'"; n.ExecuteNonQuery(); Toast.MakeText(this, "Quantity updated to: " + test.currQuantity(partnumber.Text), ToastLength.Long) .Show(); partnumber.Text = ""; partqty.Text = ""; partnumber.RequestFocus(); connection.Close(); return; }); var dialog = builder.Create(); dialog.Show(); } else { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetTitle("Error"); builder.SetMessage(ex.Message.ToString()); var dialog = builder.Create(); dialog.Show(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload