Every time when I use debugger, one of my stored procedures is executed, even when I stop the debugger before reaching the line of code responsible for saving data to the database.
How could I prevent this from happening? I don’t want to have incomplete rows in the database.
POrder po = new POrder();
po.PODate = DateTime.Parse(txtPODate.Text); <-- Breakpoint here
po.POVendorID = int.Parse(ddPOVendors.SelectedValue); <-- Stop Debugging here
I am not reaching po.AddNewOrder yet and I stop the debugger, so I am not executing the stored procedure yet
po.AddNewOrder();
POrder class:
public DateTime PODate { get; set; }
public int POVendorID { get; set; }
public void AddNewOrder()
{
SqlConnection con = new SqlConnection(ss.GetConectionString());
SqlCommand cmd = new SqlCommand("uspAddNewPOrder", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parPODate = new SqlParameter("@PODate", SqlDbType.Date);
parPODate.Value = PODate;
cmd.Parameters.Add(parPODate);
SqlParameter parPOVendorID = new SqlParameter("@POVendorID", SqlDbType.Int);
parPOVendorID.Value = POVendorID;
cmd.Parameters.Add(parPOVendorID);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
throw new Exception(exp.Message.ToString());
}
finally
{
cmd.Dispose();
con.Close();
}
}
Stored procedure code:
ALTER PROCEDURE [dbo].[uspAddNewPOrder]
(
@PODate datetime = null,
@POVendorID int= null
)
AS
BEGIN
INSERT INTO PurchaseOrders.[dbo].[tblPurchaseOrders]
(
PODate,
POVendorID
)
VALUES
(
@PODate,
@POVendorID
)
END
You can use
preprocessor directivesto skip a sections of code executing given a condition (ex; during debugging in your case). Have a look at this MSDN link